Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

LXF#126 Shell scripting explanation

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
Red Bow Tie



Joined: Sun Mar 06, 2011 11:20 pm
Posts: 4

PostPosted: Mon Mar 07, 2011 12:14 am    Post subject: LXF#126 Shell scripting explanation Reply with quote

Trying to follow the script Bob Moss has on pg 48-51. Just a beginner so please have patience. When I get to point
var=$1
echo $var

and then type

sh script_tutorial.sh ''Hello,World\!''

I must assume that the quotes here are two single quotes at the beginning of the Hello,World string and at the end of the line. If I try double quotes here I get

Hello,World\!

and on the next step, sending the value of the argument ($1) to the file sample.txt results in a blank file.
It does work with two single quotes, though. Is this correct? Why is it necessary to escape the ! mark Question
Back to top
View user's profile Send private message
kvonh



Joined: Tue Apr 12, 2005 10:46 pm
Posts: 19

PostPosted: Wed Mar 09, 2011 11:20 am    Post subject: Reply with quote

Quoting is a bit of a dark art ... but persist and you will quickly become the expert ...

"double quotes" allow certain things within the quoted string to be be expanded. The best example is shell variables, so for example if you do something like this:

NAME=Red
echo "Hello $NAME"

You'll get

Hello Red

'Single quotes' however, cause (almost) everything to be passed through unchanged. Thus:

NAME=Red
echo 'Hello $NAME'

will print

Hello $NAME

Now, to answer your question ... The ! character is used by the shell to reference back to previously executed commands. If you type

history

into the shell, you'll see a numbered list of commands that you have previously entered. If you type at the command prompt:

!123

It will rerun command number 123 from your history. Similarly, if you type

!foo

It will rerun the most-recently-entered command that starts with the string foo.

When you use '!' in single quotes, it's special meaning is lost, it just goes through unchanged. Similarly the \ before it: that is passed unchanged, so

echo '\!'

just prints

\'

Now if you use "double quotes" in the same example, it does this (because the \ hides the ! from the shell, conceptually):

echo "\!"
\!

But here's the punchline (note, there is no \backslash here):

echo "!"

fails because the shell cannot find an event in your history to match the !

So, in summary: Bob added a backslash to remove the meaning of the ! He would perhaps have done better to use single quotes (and no backslash).

But that could have caused problems in his script, because in certain cases the shell will *reinterpret* values. The best/safest bet (imho) is to wrap every reference to a variable in "double quotes", thus from Bob's script:

var="$1"
echo "$var"

followed by:

sh script_tutorial 'Hello, World!'

This should Just Work

I hope this helps.

James
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast