<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0">
<channel>
  <title>Linux Format forums</title>
  <link>http://www.linuxformat.com/forums/index.php</link>
  <description>Help, discussion, magazine feedback and more</description>
  <language>english</language>
  <copyright>(c) Copyright Wed Jun 19, 2013 10:19 pm by Linux Format forums</copyright>
  <managingEditor>webmaster@linuxformat.com</managingEditor>
  <webMaster>webmaster@linuxformat.com</webMaster>
  <pubDate>Wed Jun 19, 2013 10:19 pm</pubDate>
  <lastBuildDate>Wed Jun 19, 2013 10:19 pm</lastBuildDate>
  <docs>http://backend.userland.com/rss</docs>
  <generator>phpBB2 RSS Syndication Mod by Lucas</generator>
  <ttl>1</ttl>

  <image>
    <title>Linux Format forums</title>
    <url></url>
    <link>http://www.linuxformat.com/forums/</link>
    <description>Help, discussion, magazine feedback and more</description>
  </image>

                                      <item>
                                        <title>Re: LXF#126 Shell scripting explanation</title>
                                        <link>http://www.linuxformat.com/forums/viewtopic.php?p=97572#97572</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://www.linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=305'&gt;kvonh&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Wed Mar 09, 2011 11:20 am&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      Quoting is a bit of a dark art ... but persist and you will quickly become the expert ...&lt;br /&gt;
&lt;br /&gt;
&amp;quot;double quotes&amp;quot; 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:&lt;br /&gt;
&lt;br /&gt;
 NAME=Red&lt;br /&gt;
 echo &amp;quot;Hello $NAME&amp;quot;&lt;br /&gt;
&lt;br /&gt;
You'll get&lt;br /&gt;
&lt;br /&gt;
 Hello Red&lt;br /&gt;
&lt;br /&gt;
'Single quotes' however, cause (almost) everything to be passed through unchanged.  Thus:&lt;br /&gt;
&lt;br /&gt;
 NAME=Red&lt;br /&gt;
 echo 'Hello $NAME'&lt;br /&gt;
&lt;br /&gt;
will print&lt;br /&gt;
&lt;br /&gt;
 Hello $NAME&lt;br /&gt;
&lt;br /&gt;
Now, to answer your question ... The ! character is used by the shell to reference back to previously executed commands.  If you type&lt;br /&gt;
&lt;br /&gt;
 history&lt;br /&gt;
&lt;br /&gt;
into the shell, you'll see a numbered list of commands that you have previously entered.  If you type at the command prompt:&lt;br /&gt;
&lt;br /&gt;
 !123&lt;br /&gt;
&lt;br /&gt;
It will rerun command number 123 from your history.  Similarly, if you type&lt;br /&gt;
&lt;br /&gt;
 !foo&lt;br /&gt;
&lt;br /&gt;
It will rerun the most-recently-entered command that starts with the string foo.&lt;br /&gt;
&lt;br /&gt;
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 &lt;br /&gt;
&lt;br /&gt;
 echo '\!'&lt;br /&gt;
&lt;br /&gt;
just prints&lt;br /&gt;
&lt;br /&gt;
 \'&lt;br /&gt;
&lt;br /&gt;
Now if you use &amp;quot;double quotes&amp;quot; in the same example, it does this (because the \ hides the ! from the shell, conceptually):&lt;br /&gt;
&lt;br /&gt;
 echo &amp;quot;\!&amp;quot;&lt;br /&gt;
 \!&lt;br /&gt;
&lt;br /&gt;
But here's the punchline (note, there is no \backslash here):&lt;br /&gt;
&lt;br /&gt;
 echo &amp;quot;!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
fails because the shell cannot find an event in your history to match the !&lt;br /&gt;
&lt;br /&gt;
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).&lt;br /&gt;
&lt;br /&gt;
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 &amp;quot;double quotes&amp;quot;, thus from Bob's script&amp;#058;&lt;br /&gt;
&lt;br /&gt;
var=&amp;quot;$1&amp;quot;&lt;br /&gt;
echo &amp;quot;$var&amp;quot;&lt;br /&gt;
&lt;br /&gt;
followed by:&lt;br /&gt;
&lt;br /&gt;
sh script_tutorial 'Hello, World!'&lt;br /&gt;
&lt;br /&gt;
This should Just Work&lt;br /&gt;
&lt;br /&gt;
I hope this helps.&lt;br /&gt;
&lt;br /&gt;
James</description>
                                        <comments>http://www.linuxformat.com/forums/viewtopic.php?p=97572#97572</comments>
                                        <author>kvonh</author>
                                        <pubDate>Wed Mar 09, 2011 11:20 am</pubDate>
                                        <guid isPermaLink="true">http://www.linuxformat.com/forums/viewtopic.php?p=97572#97572</guid>
                                      </item>
                                      <item>
                                        <title>LXF#126 Shell scripting explanation</title>
                                        <link>http://www.linuxformat.com/forums/viewtopic.php?p=97473#97473</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://www.linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=67286'&gt;Red Bow Tie&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Mon Mar 07, 2011 12:14 am&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      Trying to follow the script Bob Moss has on pg 48-51. Just a beginner so please have patience. When I get to point &lt;br /&gt;
var=$1&lt;br /&gt;
echo $var&lt;br /&gt;
&lt;br /&gt;
and then type &lt;br /&gt;
&lt;br /&gt;
sh script_tutorial.sh ''Hello,World\!''&lt;br /&gt;
&lt;br /&gt;
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 &lt;br /&gt;
 &lt;br /&gt;
Hello,World\!&lt;br /&gt;
&lt;br /&gt;
and on the next step, sending the value of the argument ($1) to the file sample.txt results in a blank file.&lt;br /&gt;
It does work with two single quotes, though. Is this correct?  Why is it necessary to escape the ! mark &lt;img src=&quot;images/smiles/icon_question.gif&quot; alt=&quot;Question&quot; border=&quot;0&quot; /&gt;</description>
                                        <comments>http://www.linuxformat.com/forums/viewtopic.php?p=97473#97473</comments>
                                        <author>Red Bow Tie</author>
                                        <pubDate>Mon Mar 07, 2011 12:14 am</pubDate>
                                        <guid isPermaLink="true">http://www.linuxformat.com/forums/viewtopic.php?p=97473#97473</guid>
                                      </item></channel></rss>