LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-08-2009, 07:32 AM   #1
rajesh486
LQ Newbie
 
Registered: Jan 2009
Posts: 2

Rep: Reputation: 0
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
 
Old 01-08-2009, 07:46 AM   #2
mk27
Member
 
Registered: Sep 2008
Distribution: fedora, gentoo, ubuntu
Posts: 148

Rep: Reputation: 23
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

Last edited by mk27; 01-08-2009 at 07:48 AM.
 
Old 01-08-2009, 07:48 AM   #3
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
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
 
Old 01-08-2009, 07:52 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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\!\!
 
Old 01-09-2009, 12:23 PM   #5
slack12ware
Member
 
Registered: Mar 2008
Location: Deep in the Jango(Africa)
Distribution: Slackware 12, Fedora 8
Posts: 46

Rep: Reputation: 15
I think This Book could also be helpful in further explaining the above question and your bash scripting.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Programming eantoranz Linux - Networking 2 06-02-2005 12:21 PM
Shell Programming sharadgana Programming 5 12-14-2004 08:42 AM
shell programming athenerx Programming 1 10-28-2001 05:33 AM


All times are GMT -5. The time now is 05:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration