Quotes are used when assigning values containing whitespace or special characters, to delimit parameters and variables, and to assign command output.
There are three types of quotes: single quotes, double quotes, and back quotes. Single and double quotes are similar, except for the way they handle some special characters. Back quotes are used for command output assignment.
Look what happens when you try to perform a variable assignment using a value that contains whitespace without enclosing it in quotes:
$ GREETING=Hello world /bin/ksh: world: not found $ print $GREETING $
No assignment is made. To assign Hello world to GREETING, you would need to enclose the entire string in quotes, like this:
$ GREETING='Hello world' $ print $GREETING Hello world
Single quotes are also used to hide the meaning of special characters like $, *, \, !, ", ` and /. Any characters between single quotes, except another single quote, are displayed without interpretation as special characters:
$ print '* $ \ ! ` / "' * $ \ ! ` / "
This also means that variable and command substitution does not take place within single quotes (because $ and `` lose their special meaning). If you want to access the value of a variable, use double quotes instead of single quotes (discussed in the next section). So, instead of displaying /home/anatole, we get $HOME:
$ print '$HOME' $HOME
and instead of the date, we get `date`:
$ print 'Today is `date`' Today is `date`
Korn shell command substitution $(...) also doesn't work in single quotes, because of the $ character. You may be thinking what good are single quotes anyway? Well, there are still some good uses for them. You could print a menu border like this:
$ print '******************MENU*****************' ******************MENU******************
or use the $ character as the dollar symbol:
$ print 'Pass GO — Collect $200' Pass GO — Collect $200
You couldn't do that with double quotes! Actually there are quite a few good uses for single quotes. They can be used to print a double-quoted string:
$ print '"This is in double quotes"' "This is in double quotes"
or just to surround plain old text that has embedded whitespace. This improves readability by separating the command from the argument. Variables can be set to null with single quotes:
Single quotes are also used to assign values to aliases and trap commands, and prevent alias substitution, but we'll get to that later.
Double quotes are like single quotes, except that they do not remove the meaning of the special characters $, `, and \. This means that variable and command substitution is performed.
$ DB="$HOME:`pwd`" $ print $DB /home/anatole:/tmp
Double quotes also preserve embedded whitespace and newlines. Here are some examples:
$ print "Line 1 > Line 2" Line 1 Line 2 $ print "A B" A B
The > is the secondary prompt, and is displayed whenever the Korn shell needs more input. In this case, it waits for the closing double quote:
$ ADDR="ASP,Inc > PO Box 23837 > San Jose CA 95153 USA > (800)777-UNIX * (510)531-5615"
Without double quotes around ADDR, we get this:
$ print $ADDR ASP,Inc PO Box 23837 San Jose CA 95153 USA (800) 777- UNIX * (510)531-5615
Not quite what we wanted. Let's try it again:
$ print "$ADDR" ASP,Inc PO Box 23837 San Jose CA 95153 USA (800)777-UNIX * (510)531-5615
There are also other uses for double quotes. You can set a variable to null:
$ NULL="" $ print $NULL $
or display single quotes.
$ print "'This is in single quotes'" 'This is in single quotes'
If you really wanted to display the $, `, \, or " characters using double quotes, escape them with a backslash like this:
$ print "\$HOME is set to $HOME" $HOME is set to /home/anatole $ print "\`=back-quote \\=slash \"=double-quote" `=back-quote \=slash "=double-quote
Back quotes are used to assign the output of a command to a variable. This format, from the Bourne shell, is accepted by the Korn shell but considered obsolescent. This command sets the variable SYS to the system name:
$ SYS=`uuname —l` $ print $SYS aspd