VMS-to-Unix Phrase Book
<=  Return           

1.5  Quoting

Problem

You want to stop the shell from interpreting special characters, or you want to include shell variables as part of a text string.

Solution

Use strong quotes ('string') to prevent protect special characters from the shell; quote marks ("string") when you want shell variables to be de-referenced, and back ticks (`cmd`) when you want to execute a command and have it's STDOUT text inserted into the command line.

Discussion

Before we discuss each type of quoting, consider the following examples:

VMS Unix
vms> name="direct/nohead/notrail *.for
vms> write sys$output "name"
name
vms> write sys$output name
directory/nohead/notrail *.for
vms> 'name'
DISK$GU02:[W_COSTA]PROG1.FOR;1
DISK$GU02:[W_COSTA]PROG2.FOR;1
DISK$GU02:[W_COSTA]PROG3.FOR;1
vms>
unix> name='ls *.f'
unix> echo '$name'
$name
unix> echo "$name"
ls *.f
unix> echo $name
ls prg1.f prg2.f prg3.f
unix> echo `$name`
prg1.f prg2.f prg3.f
unix>

Strong quotes ('string') protect the enclosed string from any interpretation by the shell. In the first two lines, we use the strong quotes to get the string into the variable without any interpretation of the * wildcard character, and then to display the string $name without having it interpreted as a shell variable invocation. Strong quotes allow you to defer all substitutions till some later time.

unix> alias mypgms='ls *.f'
unix> cd New
unix> mypgms
prg1.f  prg2.f  prg3.f
unix> cd ../Old
unix> mypgms
that.f   this.f   those.f
unix>

Quote marks (a.k.a. double quotes) allow the expansion of variables within the context of the string. This is a common way to encorporate variable contents as part of a message string which is demonstrated in the second line of the following example:

unix> count=`ls -1 *.f | wc -l`
unix> echo "There are $count Fortran files."
There are          3 Fortran files.
unix>

In the above example, we also demonstrated the use of the backtick or accent grave (`command`) marks for command substitution. Enclosing a shell command inside of backticks causes the command to be executed and the resulting STDOUT text to be substituted in place of the quoted command. In the above example, we did a listing, one per line (-1 i.e. minus one), of all files matching *.f and piped the resulting list into the word count program, asking for only a count of lines (-l i.e. minus el). The resulting text, including all the leading spaces, was assigned to the shell variable count.

Finally, the backslash (\) character is used to prevent the interpretation of a special character where it would normally be acted upon by the shell. This special character escape mechanism can be used both inside and outside of all the above quoting mechanisms.

unix> count=5
unix> echo "\$count = $count"
$count = 5
unix> echo "He asked, \"How are you?\""
He asked, "How are you?"
unix> ls *.f
that.f   this.f   those.f
unix> ls \*.f
ls: *.f not found
unix>

See Also

1.3 - Aliases;
1.4 - Symbols and Logicals;
Chapter 10 of Unix for OpenVMS Users ;
Chapter 9 of Unix Power Tools .


<=  Return           

Colophon:
Best Viewed With Any Browser
This page maintained by:
    Bill.Costa@unh.edu
    of the Enterprise Computing Group
    in the dept of Computing & Information Sevices
    at the University of New Hampshire

Typographical
Conventions

Created:  31-Jan-2001 BC
Revised:  27-Mar-2001 BC