Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting | ||
---|---|---|
Prev | Chapter 5. Quoting | Next |
Escaping is a method of quoting single characters. The escape (\) preceding a character tells the shell to interpret that character literally.
With certain commands and utilities, such as echo and sed, escaping a character may have the opposite effect - it can toggle on a special meaning for that character. |
Special meanings of certain escaped characters
means newline
means return
means tab
means vertical tab
means backspace
means alert (beep or flash)
translates to the octal ASCII equivalent of 0nn, where nn is a string of digits
Example 5-2. Escaped Characters
1 #!/bin/bash 2 # escaped.sh: escaped characters 3 4 echo; echo 5 6 # Escaping a newline. 7 # ------------------ 8 9 echo "" 10 11 echo "This will print 12 as two lines." 13 # This will print 14 # as two lines. 15 16 echo "This will print \ 17 as one line." 18 # This will print as one line. 19 20 echo; echo 21 22 echo "=============" 23 24 25 echo "\v\v\v\v" # Prints \v\v\v\v literally. 26 # Use the -e option with 'echo' to print escaped characters. 27 echo "=============" 28 echo "VERTICAL TABS" 29 echo -e "\v\v\v\v" # Prints 4 vertical tabs. 30 echo "==============" 31 32 echo "QUOTATION MARK" 33 echo -e "\042" # Prints " (quote, octal ASCII character 42). 34 echo "==============" 35 36 # The $'\X' construct makes the -e option unnecessary. 37 echo; echo "NEWLINE AND BEEP" 38 echo $'\n' # Newline. 39 echo $'\a' # Alert (beep). 40 41 echo "===============" 42 echo "QUOTATION MARKS" 43 # Version 2 and later of Bash permits using the $'\nnn' construct. 44 # Note that in this case, '\nnn' is an octal value. 45 echo $'\t \042 \t' # Quote (") framed by tabs. 46 47 # It also works with hexadecimal values, in an $'\xhhh' construct. 48 echo $'\t \x22 \t' # Quote (") framed by tabs. 49 # Thank you, Greg Keraunen, for pointing this out. 50 # Earlier Bash versions allowed '\x022'. 51 echo "===============" 52 echo 53 54 55 56 57 58 # Assigning ASCII characters to a variable. 59 # ---------------------------------------- 60 quote=$'\042' # " assigned to a variable. 61 echo "$quote This is a quoted string, $quote and this lies outside the quotes." 62 63 echo 64 65 # Concatenating ASCII chars in a variable. 66 triple_underline=$'\137\137\137' # 137 is octal ASCII code for '_'. 67 echo "$triple_underline UNDERLINE $triple_underline" 68 69 echo 70 71 ABC=$'\101\102\103\010' # 101, 102, 103 are octal A, B, C. 72 echo $ABC 73 74 echo; echo 75 76 escape=$'\033' # 033 is octal for escape. 77 echo "\"escape\" echoes as $escape" 78 # no visible output. 79 80 echo; echo 81 82 exit 0 |
See Example 34-1 for another example of the $' ... ' string-expansion construct.
gives the quote its literal meaning
1 echo "Hello" # Hello 2 echo "\"Hello\" ... he said." # "Hello" ... he said. |
gives the dollar sign its literal meaning (variable name following \$ will not be referenced)
1 echo "\$variable01" # $variable01 2 echo "The book cost \$7.98." # The book cost $7.98. |
gives the backslash its literal meaning
1 echo "\\" # Results in \ 2 3 # Whereas . . . 4 5 echo "\" # Invokes secondary prompt from the command line. 6 # In a script, gives an error message. 7 8 # However . . . 9 10 echo '\' # Results in \ |
The behavior of \ depends on whether it is escaped, strong-quoted, weak-quoted, or appearing within command substitution or a here document.
Elements of a string assigned to a variable may be escaped, but the escape character alone may not be assigned to a variable.
|
Escaping a space can prevent word splitting in a command's argument list.
1 file_list="/bin/cat /bin/gzip /bin/more /usr/bin/less /usr/bin/emacs-20.7" 2 # List of files as argument(s) to a command. 3 4 # Add two files to the list, and list all. 5 ls -l /usr/X11R6/bin/xsetroot /sbin/dump $file_list 6 7 echo "-------------------------------------------------------------------------" 8 9 # What happens if we escape a couple of spaces? 10 ls -l /usr/X11R6/bin/xsetroot\ /sbin/dump\ $file_list 11 # Error: the first three files concatenated into a single argument to 'ls -l' 12 # because the two escaped spaces prevent argument (word) splitting. |
The escape also provides a means of writing a multi-line command. Normally, each separate line constitutes a different command, but an escape at the end of a line escapes the newline character, and the command sequence continues on to the next line.
1 (cd /source/directory && tar cf - . ) | \ 2 (cd /dest/directory && tar xpvf -) 3 # Repeating Alan Cox's directory tree copy command, 4 # but split into two lines for increased legibility. 5 6 # As an alternative: 7 tar cf - -C /source/directory . | 8 tar xpvf - -C /dest/directory 9 # See note below. 10 # (Thanks, Stéphane Chazelas.) |
If a script line ends with a |, a pipe character, then a \, an escape, is not strictly necessary. It is, however, good programming practice to always escape the end of a line of code that continues to the following line. |
1 echo "foo 2 bar" 3 #foo 4 #bar 5 6 echo 7 8 echo 'foo 9 bar' # No difference yet. 10 #foo 11 #bar 12 13 echo 14 15 echo foo\ 16 bar # Newline escaped. 17 #foobar 18 19 echo 20 21 echo "foo\ 22 bar" # Same here, as \ still interpreted as escape within weak quotes. 23 #foobar 24 25 echo 26 27 echo 'foo\ 28 bar' # Escape character \ taken literally because of strong quoting. 29 #foo\ 30 #bar 31 32 # Examples suggested by Stéphane Chazelas. |