Shell Programming : $1 $2 ..
Hi all,
I just started off learning shell scripting basics and am an absolute newbie.. These are the commands i typed on the bash shell.. $set I am the king $echo $1 $2 $3 $4 I am the king // output of the above command $echo $1 $2 $3 $4 who do u think u r!! echo $1 $2 $3 $4 who do u think u recho $1 $2 $3 $4 //output I am the king who do u think u recho I am the king //output While i expected it to print "I am the king who do u think u r!!" It printed something which i found gibberish. Why is the output so? i even tried using a '?' but it prints just fine and also it works as expected with a '!'. Please clarify my doubt.. Regards, RJ |
There's two reasons for this. The main one is that !! refers to the last command. Apparently the shell first shows what it is, then executes it in context:
Code:
prompt: echo this Code:
prompt: echo echo If you want to actually see !! use single quotes: Code:
prompt: echo the emperor has new clothes'!!' :jawa: |
Hi.
The '!' character is used to find the last command you used which matches a string, or to execute the last command in the case of '!!' To print literal '!' characters, you'll have to either escape them: $ echo hello\!\! or quote the string you want to echo: $ echo 'hello!!' To be honest I can't quite figure out exactly what's happening with the '!!' at the end of a line - seems to be doing odd things with the shell history, but I'm not sure what. Dave |
It happens because the double exclamation mark is expanded by the shell as "the last executed command" (see the BASH history explanation in your guide). When you use !! the shell first prints out the command to execute, then the output of the command itself. For example:
Code:
$ whoami Code:
$ set I am the king Code:
$ echo $1 $2 $3 $4 who do u think u r'!!' |
I think This Book could also be helpful in further explaining the above question and your bash scripting.
|
All times are GMT -5. The time now is 01:01 AM. |