| Chapter 5. Quoting
Quoting means just that, bracketing a string in quotes. Thishas the effect of protecting specialcharacters in the string from reinterpretationor expansion by the shell or shell script. (A characteris "special" if it has an interpretationother than its literal meaning. For example, the asterisk * representsa wild card character in globbing and Regular Expressions). bash$ ls -l [Vv]*-rw-rw-r-- 1 bozo bozo 324 Apr 2 15:05 VIEWDATA.BAT -rw-rw-r-- 1 bozo bozo 507 May 4 14:25 vartrace.sh -rw-rw-r-- 1 bozo bozo 539 Apr 14 17:11 viewdata.shbash$ ls -l '[Vv]*'ls: [Vv]*: No such file or directory |
Certain programs and utilities reinterpret or expandspecial characters in a quoted string. An important use ofquoting is protecting a command-line parameter from the shell,but still letting the calling program expand it. bash$ grep '[Ff]irst' *.txtfile1.txt:This is the first line of file1.txt. file2.txt:This is the First line of file2.txt. | Note that the unquoted grep [Ff]irst *.txt works under the Bash shell. Quoting can also suppress echo's "appetite" for newlines. bash$ echo $(ls -l)total 8 -rw-rw-r-- 1 bo bo 13 Aug 21 12:57 t.sh -rw-rw-r-- 1 bo bo 78 Aug 21 12:57 u.shbash$ echo "$(ls -l)"total 8 -rw-rw-r-- 1 bo bo 13 Aug 21 12:57 t.sh -rw-rw-r-- 1 bo bo 78 Aug 21 12:57 u.sh |
5.1. Quoting VariablesWhen referencing a variable, it is generally advisable toenclose its name in double quotes.This prevents reinterpretation of all special characters withinthe quoted string -- except $, `(backquote), and (escape). Keeping $ as a special character withindouble quotes permits referencing a quoted variable("$variable"), that is, replacing thevariable with its value (see Example 4-1, above).
Use double quotes to prevent word splitting. An argument enclosed in double quotes presentsitself as a single word, even if it contains whitespace separators.
List="one two three" for a in $List # Splits the variable in parts at whitespace.do echo "$a" done# one# two# threeecho "---" for a in "$List" # Preserves whitespace in a single variable.do # ^ ^ echo "$a" done# one two three | A more elaborate example: variable1="a variable containing five words" COMMAND This is $variable1 # Executes COMMAND with 7 arguments:# "This" "is" "a" "variable" "containing" "five" "words" COMMAND "This is $variable1" # Executes COMMAND with 1 argument:# "This is a variable containing five words" variable2="" # Empty.COMMAND $variable2 $variable2 $variable2 # Executes COMMAND with no arguments. COMMAND "$variable2" "$variable2" "$variable2" # Executes COMMAND with 3 empty arguments. COMMAND "$variable2 $variable2 $variable2" # Executes COMMAND with 1 argument (2 spaces). # Thanks, St�phane Chazelas. | | Enclosing the arguments to an echostatement in double quotes is necessary only when word splittingor preservation of whitespaceis an issue. | Example 5-1. Echoing Weird Variables #!/bin/bash# weirdvars.sh: Echoing weird variables.echovar="'(]{}$"" echo $var # '(]{}$" echo "$var" # '(]{}$" Doesn't make a difference.echoIFS=''echo $var # '(] {}$" converted to space. Why?echo "$var" # '(]{}$" # Examples above supplied by Stephane Chazelas.echovar2=""" echo $var2 # " echo "$var2" # " echo# But ... var2=""" is illegal. Why?var3=''echo "$var3" # # Strong quoting works, though.# ************************************************************ ## As the first example above shows, nesting quotes is permitted.echo "$(echo '"')" # " # ^ ^# At times this comes in useful.var1="Two bits" echo "$var1 = "$var1"" # $var1 = Two bits# ^ ^# Or, as Chris Hiestand points out ...if [[ "$(du "$My_File1")" -gt "$(du "$My_File2")" ]]; then ...# ************************************************************ # | Single quotes (' ') operate similarly to doublequotes, but do not permit referencing variables, sincethe special meaning of $ is turned off.Within single quotes, every specialcharacter except ' gets interpreted literally.Consider single quotes ("full quoting") to be astricter method of quoting than double quotes ("partialquoting"). | Since even the escape character ()gets a literal interpretation within single quotes, trying toenclose a single quote within single quotes will not yield theexpected result. echo "Why can't I write 's between single quotes" echo# The roundabout method.echo 'Why can'''t I write '"'"'s between single quotes'# |-------| |----------| |-----------------------|# Three single-quoted strings, with escaped and quoted single quotes between.# This example courtesy of St�phane Chazelas. | |
5.2. EscapingEscaping is a methodof quoting single characters. The escape() preceding a character tells the shell tointerpret that character literally. | With certain commands and utilities, such as echo and sed, escaping a character may have theopposite effect - it can toggle on a special meaning for thatcharacter. | Special meanings of certainescaped characters - used with echo andsed
means newline means return means tab - v
means vertical tab means backspace - a
means alert (beep or flash)
|
| |
|