Unix tcsh is an alias using the $ 1 command line argument compared to \ !: 1

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

But I noticed that if I try to save $1 into an environment variable, it will not be saved. However, \!:1 preserved.

 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 is echoed in $ x when $ 1 is used to store the value. What is the reason for this?

+6
source share
2 answers

$1 returns the first argument passed to the script that contains the alias command. Therefore, if you call it from the command line, it will not return anything.

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

+14
source

supergra answered the main question, but this may make you wonder why you see that your text was repeated to you, although the variable was not set. That is, you have echo $x at the end of your alias, and indeed you see no when entering oh no , but this does not mean that the echo is reflected in the variable.

What happens is that echo prints the (empty) variable, but then echo also captures the "no" part separately. If you do alias tmp 'echo $1' and try tmp hi , you will say hello because it is as if you made "echo $ 1 hi".

To make this clearer, try alias tmp 'echo abc $1 def ' and again tmp hi and you will type "abc def hi". Again, if you try alias tmp 'echo $1 & which ' and use it again, you should if you don't have a command named hi , see Something like "hello: command not found". or if you run tmp ls , you will see the output of which ls .

Another example: try alias tmp 'echo $1 & ' and tmp hi to make sure that it is really trying to execute hi , as if it were a command that could be dangerous if you weren't expecting it.

+5
source

Source: https://habr.com/ru/post/956331/


All Articles