LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell Programming : $1 $2 .. (https://www.linuxquestions.org/questions/linux-newbie-8/shell-programming-%241-%242-695828/)

rajesh486 01-08-2009 06:32 AM

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

mk27 01-08-2009 06:46 AM

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
this
prompt: !!
echo this
this

The second is because you can do this:
Code:

prompt: echo echo
echo

So you included the previous command as a string literal in an echo statement.

If you want to actually see !! use single quotes:
Code:

prompt: echo the emperor has new clothes'!!'
the emperor has new clothes!!

There's more about this in the bash man page under Event Designators. Now try ?!echo
:jawa:

ilikejam 01-08-2009 06:48 AM

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

colucix 01-08-2009 06:52 AM

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
colucix
$ !!
whoami
colucix

In your case you get the reprint of the command line with !! expanded as the last executed command (in blue below), then its output (the output of the entire echo command - in red below).
Code:

$ set I am the king
$ echo $1 $2 $3 $4
I am the king
$ 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
I am the king who do u think u recho I am the king

To prevent the shell to interpret !! as a history command, you have to use single quotes or escape the exclamation marks using a backslash. That is both the following commands should work as you expected:
Code:

$ echo $1 $2 $3 $4 who do u think u r'!!'
$ echo $1 $2 $3 $4 who do u think u r\!\!


slack12ware 01-09-2009 11:23 AM

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.