Quotes and Other Special Characters


Backslash

The backslash character (\) has two uses (note this differs from the frontslash (/) character). The first use is for line continuation. If a line of shell commands becomes exceedingly long, it may be useful to continue it across more than one line. Note that if you choose to do this, the split cannot be in the middle of a word, it must be done in appropriate whitespace.

The second (and perhaps more useful) use of the backslash character is to remove the special meaning of the following single character. This is sometimes referred to as escaping the special meaning of the following character. Characters that have special meaning are referred to as metacharacters (there are many of these in Unix). We saw in the previous section that performing multiplication using expr required the \ to be used before the * character. This is because the * is a metacharacter, and without using the backslash to remove its special meaning, an error would result. For example, if we wanted to output a statement to describe the use of the $? variable, we could try and observe:

	$ echo The $? variable returns the child exit status [Enter]
	The 0 variable returns the child exit status
where what we really want is:
	$ echo The \$? variable returns the child exit status [Enter]
	The $? variable returns the child exit status

Single Quotes

Single quotes (not to be confused with the back quotes below) serve to remove (escape) the special meaning of all characters they surround (or enclose). Thus, we could also accomplish the above as follows:
	$ echo 'The $? variable returns the child exit status' [Enter]
	The $? variable returns the child exit status

Double Quotes

Double quotes or front quotes (again, not to be confused with the back quotes or single quotes) serve to remove (escape) the special meaning of all characters they enclose, except for the $ (dollar sign) character, the \ (backslash) character, and the ` (back quote character). Thus, the following statement (from above) would work as follows:
	$ echo "The $? variable returns the child exit status" [Enter]
	The 0 variable returns the child exit status
since the $ is not escaped. An example of when this might be used is the following:
	$ echo "****** this is text surrounded by stars ******" [Enter]
	****** this is text surrounded by stars ******
It is the practice of the author to always enclose text and variables within double quotes, and escape any special characters using the backslash, as follows:
	$ echo "The \$? variable returns the child exit status" [Enter]
	The $? variable returns the child exit status

Note that if any pair of quotes is unmatched (missing either of the quotes), a situation may arise where a command results in single greater than (>) character displayed follows:

	$ echo "****** this is text surrounded by stars ****** [Enter]
	>
The > character in this instance is the shell environment variable named PS2 (prompt string 2). Do not confuse this with an output redirection character (see next section). When this occurs, the shell is trying to parse the command entered and is missing one or more characters it needs to complete its parsing. If you understand what is missing, you may be able to recover at the > prompt by typing the characters missing. Otherwise, you may want to punt with a [Ctrl-c] sequence.

Back Quotes

The back quotation mark character1  (i.e. `) when used in pairs enclosing a command serve to perform command substitution. That is, when used as follows:
	`command`
the output of command is substituted at the location of the leftmost backquote. Note that these are not the same character as the single quote mark. For example, if we wanted to output:
	My current directory is: current directory location
we could try the following:
	$ echo My current directory is: pwd [Enter]
But this would result in:
	My current directory is: pwd
To achieve what we wish, we could use back quotes as follows:
	$ echo My current directory is: `pwd` [Enter]
	My current directory is: /home/mthomas
Note the / in /home is substituted exactly at the location of the leftmost backquote. We can also perform assignment using the backquote characters, for example:
	$ CUR_DIR=`pwd` [Enter]					# note no spaces around the =
	$ echo My current directory is: $CUR_DIR [Enter]	# right
	My current directory is: /home/mthomas

	$ echo 'My current directory is: $CUR_DIR' [Enter]	# wrong, see above
	My current directory is: $CUR_DIR
or with respect to the expr command:
	$ I=10 [Enter]
	$ I=`expr $I + 1` [Enter]
	$ echo $I [Enter]
11
An alternative notation for command substitution (present in more modern shells) is the $(command) syntax. This enables one do the following:
	$ echo My current directory is: $(pwd) [Enter]
	My current directory is: /home/mthomas

1 Technically, a single one of these is called a grave accent, but are often informally referred to as backticks, or back tick marks.


Quote Summary

 Symbol(s)  Name Description
'...' single quote remove special meaning of all enclosed characters
"..." double quote remove special meaning of all enclosed characters except $ ' and \
\ backslash removes special meaning of single following character
\ backslash also used for line continuation at the end of a long line
`command` back quote execute command and insert standard output at the (exact) location of the left-most back quote


Command Summary




©2021, Mark A. Thomas. All Rights Reserved.