Why:
you will need to quote (specially delimit)
some characters and strings, when constructing
other strings your script needs.
there are four ways to quote strings in the bourne shell.
each of these is useful in some contexts. The four ways
to quote text use: [1] single quotes [2] double quotes
[3] backquotes [4] backslashes
example, suppose you have a script called 'my1'.
This script
#!/bin/sh
foo=hello
echo $foo
when run, prints out
hello
but observe that the script
#!/bin/sh
foo=hello there
echo $foo
doesn't print
hello there
but gives an error
./my1: there: not found
the problem is that the script doesn't recognize 'there'
as part of the string value of the variable foo. the
easy way to get around this, is to quote the left-hand-side
of the assignment to foo:
#!/bin/sh
foo='hello there'
echo $foo
notice that those quoting symbols are forward-slanting single
quotes. now when ya run the script, you get
hello there
now, suppose you want to substitute the VALUE of a string
variable like 'bar' into another string. The obvious thing
to try is
#!/bin/sh
name='bubba'
foo='hello there, $name'
echo $foo
but this doesn't work quite as you'd expect. now the script
prints out
hello there, $name
but what you want is something like
hello there, bubba
with the string value of 'bubba' substituted for the variable
reference $name. single quotes quote text LITERALLY, and
variable references inside of single-quoted strings do not
get dereferenced. (dereference is a fancy word for "to take
the value of").
if you want to put variables inside of strings when you are
writing a shell script, you need to use double quotes, as in
#!/bin/sh
name='bubba'
foo="hello there, $name"
echo $foo
which, when run, prints out the intended result:
hello there, bubba
sometimes, it is useful to put into a string, the text output
of some other unix command. here's an example. suppose you
want to see just the process status (the ps command) output
just for your current login shell. your login shell has the
process id (pid) with a value that the shell automagically
stores in the $ variable. so to capture the ps command
output just for your login shell, you'd use a little script
like this:
#!/bin/sh
my_ps_output=`ps | grep $$`
echo "my shell info:"
echo $my_ps_output
note that in this script, you quote a normal unix command
(something you could type in interactively, at the shell
prompt) with backquotes. the $$ simply says "the value of
the $ variable." so the meat of this script is in the line
my_ps_output=`ps | grep $$`
which is interpreted as
[1] run the ps command.
[2] print its output into the input of the grep command.
[3] look for and print out any lines of input that match
the value of the $ variable.
[4] set the variable my_ps_output to whatever this grep command
prints out.
as you can see, there's a lot going on here!
by the way, you can put back-quoted commands inside of
double-quoted strings, and the back-quoting still works.
finally, sometimes you want to quote just a single character.
you can do this with the backslash "\" character.
#!/bin/sh
echo "#!/bin/sh
cat \$0" > $1
$1 means 'the first command-line argument to this script.'
this would be like argv[1] to a C program.
this script actually creates another script, and the
result script has whatever name you gave this script
as command-line arg 1.
Here, we want the $0 to NOT be substituted in the string.
You could have just used single-quotes around the string,
but the advantage of quoting with a backslash is that this
"hard-quotes" the minimal amount of text possible. It's
like eating a steak; you can cut the steak with either a
table knife or a chainsaw, and while the chainsaw works
all right, it's really overkill...
To review:
single quotes quote literally,
with no variable dereferencing
double quotes quote with variable or backquote
substitution
back quotes replace the string with the output
of some unix command(s)
backslash quotes a single character.
So now you know Everything There Is To Know about Quoting...
Just don't quote me on that.
-lou