سؤال

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?

هل كانت مفيدة؟

المحلول

$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.

نصائح أخرى

supergra has answered the primary point, but it may leave you wondering why you see your text echoed back to you even though the variable was not set. That is, you have echo $x at the end of your alias, and indeed you see no when you enter oh no, but that does not mean that echo is echoing the variable.

What is happening there is that echo is printing the (empty) variable, but then echo is also catching the "no" part separately. If you do alias tmp 'echo $1' and try tmp hi you will print "hi" because it's as if you did "echo $1 hi".

To see this more clearly, try alias tmp 'echo abc $1 def ' and do tmp hi again, and you will print "abc def hi". Again, if you try alias tmp 'echo $1 & which ' and use it again you should, unless you have a command named hi, see something like "hi: Command not found." or if you do tmp ls you will see the output of which ls.

One more example: try alias tmp 'echo $1 & ' and tmp hi to see that it actually tries to execute hi as if it were a command, which can be dangerous if you were not expecting that.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top