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 statuswhere what we really want is:
$ echo The \$? variable returns the child exit status [Enter] The $? variable returns the child exit status
$ echo 'The $? variable returns the child exit status' [Enter] The $? variable returns the child exit status
$ echo "The $? variable returns the child exit status" [Enter] The 0 variable returns the child exit statussince 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.
`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 locationwe could try the following:
$ echo My current directory is: pwd [Enter]But this would result in:
My current directory is: pwdTo achieve what we wish, we could use back quotes as follows:
$ echo My current directory is: `pwd` [Enter] My current directory is: /home/mthomasNote 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_DIRor with respect to the expr command:
$ I=10 [Enter] $ I=`expr $I + 1` [Enter] $ echo $I [Enter]An alternative notation for command substitution (present in more modern shells) is the $(command) syntax. This enables one do the following:
11
$ echo My current directory is: $(pwd) [Enter] My current directory is: /home/mthomas1 Technically, a single one of these is called a grave accent, but are often informally referred to as backticks, or back tick marks.
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 |
©2021, Mark A. Thomas. All Rights Reserved.