Chapter 5. Quoting. Comtrol DeviceMaster Software Development Kit
Advertisement
Advertisement
Chapter 5. Quoting
the string from reinterpretation or expansion by the shell or shell script. (A character is "special" if it has an
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.sh
bash$
ls -l '[Vv]*'
ls: [Vv]*: No such file or directory
In everyday speech or writing, when we "quote" a phrase, we set it apart and give it special meaning. In a
Bash script, when we quote a string, we set it apart and protect its literal meaning.
Certain programs and utilities reinterpret or expand special characters in a quoted string. An important use of quoting is protecting a command-line parameter from the shell, but still letting the calling program expand it.
bash$
grep '[Ff]irst' *.txt
file1.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. [29]
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.sh
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.sh
5.1. Quoting Variables
When referencing a variable, it is generally advisable to enclose its name in double quotes. This prevents reinterpretation of all special characters within the quoted string -- except $, ` (backquote), and \ (escape). [30]
Keeping $ as a special character within double quotes permits referencing a quoted variable
(
"$variable"
), that is, replacing the variable with its value (see Example 4-1, above).
Use double quotes to prevent word splitting. [31] An argument enclosed in double quotes presents itself as a
single word, even if it contains whitespace separators.
List="one two three"
Chapter 5. Quoting 41
Advanced Bash-Scripting Guide for a in $List # Splits the variable in parts at whitespace.
do
echo "$a" done
# one
# two
# three echo "---" 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 echo statement in double quotes is necessary only when word splitting or
preservation of whitespace is an issue.
Example 5-1. Echoing Weird Variables
#!/bin/bash
# weirdvars.sh: Echoing weird variables.
echo var="'(]\\{}\$\"" echo $var # '(]\{}$" echo "$var" # '(]\{}$" Doesn't make a difference.
echo
IFS='\' echo $var # '(] {}$" \ converted to space. Why?
echo "$var" # '(]\{}$"
# Examples above supplied by Stephane Chazelas.
Chapter 5. Quoting 42
Download
Advertisement