Unix tcsh - alias using command line argument $1 versus \!:1

itchmyback picture itchmyback · Oct 19, 2013 · Viewed 16.9k times · Source

In Unix (tcsh), I've referenced command line arguments in my aliases with two different notations - $1 and \!:1.

But I noticed that if I try to save $1 to an environment variable, it doesn't get saved. However \!:1 does get saved.

alias hear 'setenv x \!:1 && echo $x'
--> hear that
that
--> echo $x
that

alias oh 'setenv x $1 && echo $x'
--> oh no
no
--> echo $x

Nothing shows up on the echo of $x when $1 is used to store the value. What is the reason for this?

Answer

supergra picture supergra · Dec 15, 2013

$1 returns the first argument passed to the script that contains the alias command. So if you are calling it from the command line, it will return nothing.

\!:1 returns the first argument passed to the aliased command, so that is clearly what you should be using.