Chapter 5. Quoting: 5.1. Quoting Variables (Quotingvar - HTML) 5.2. Escaping (Escapingsection - HTML)
Chapter 5. Quoting: 5.1. Quoting Variables (Quotingvar - HTML) 5.2. Escaping (Escapingsection - HTML)
Chapter 5. Quoting: 5.1. Quoting Variables (Quotingvar - HTML) 5.2. Escaping (Escapingsection - HTML)
Prev Next
(othertypesv.html) (quotingvar.html)
Chapter 5. Quoting
Table of Contents
5.1. Quoting Variables (quotingvar.html)
5.2. Escaping (escapingsection.html)
Quoting means just that, bracketing a string in quotes. This has the effect of
protecting special characters (special-chars.html#SCHARLIST1) in the string from
reinterpretation or expansion by the shell or shell script. (A character is "special" if it
has an interpretation other than its literal meaning. For example, the asterisk *
(special-chars.html#ASTERISKREF) represents a wild card character in globbing
(globbingref.html) and Regular Expressions (regexp.html#REGEXREF)).
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.
Note that the unquoted grep [Ff]irst *.txt works under the Bash shell. [1]
Quoting can also suppress echo's (internal.html#ECHOREF) "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
Notes
[1] Unless there is a le named first in the current working directory. Yet
(quoting.html#AEN2609)another reason to quote. (Thank you, Harald Koenig, for pointing this
out.