How to alias EXIT or any CSH command

Jonathan I. Kamens jik at athena.mit.edu
Thu Apr 4 01:08:25 AEST 1991


In article <1991Apr1.165435.4687 at berlioz.nsc.com>, nelson at berlioz.nsc.com (Taed Nelson) writes:
|> I have reason to desire an alias for the CSH command EXIT.  Since
|>   the executable version doesn't exist anywhere, I can't do:
|> 	alias exit 'MY_STUFF_HERE; /usr/bin/exit'
|>   or whatever.

  The wrong ways to do it (i.e. these won't work):

	# causes an alias loop
	alias normal_exit 'exit'
	alias exit 'MY_STUFF_HERE; normal_exit'

	# doesn't allow the shell to clean up or do .logout
	alias exit 'MY_STUFF_HERE; kill -9 $$'

The silly ways to do it (i.e. posted by people who didn't know the right way,
and knew it):

	% bash
	bash$ alias exit='MY_STUFF_HERE; builtin exit'

Ways to do it that just might work, but are sub-optimal:

	# Shell might behave strangely when it gets a HUP, there's really no
	# reason to do it this way other than not understanding the shell
	# enough to do it right.
	alias exit 'MYSTUFF; kill -HUP $$'

And finally, the ways that will do the right thing (why they do the right
thing is explained below):

	set Exit=exit
	alias exit 'MY_STUFF_HERE; $Exit'

	alias exit 'echo blurg ; ""exit'

  Alias substitution happens before variables are expanded.  Alias
substitution happens before quoting is dealt with.  Therefore, the the two
solutions above prevent an alias loop by convincing the shell not to notice
that the command being run is aliased until it is too late to do substitution.
Despite what the person who posted the last solution above said, it *does*
make sense.

  Now, as for what I think is the "best" way to do it:

	alias exit 'MY_STUFF_HERE; \exit'

Back-slashing a character quotes it, which makes the shell treat it
differently when doing alias expansion.  This is how I've always seen alias
loops avoided.  Using "" is just about the same.  Using a variable to put the
variable in is just a bit of overkill.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik at Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710



More information about the Comp.unix.questions mailing list