- The shell actually has three different quoting mechanisms:
- Single quotes
- Backslashes
- Double quotes
Nguyen Hai Chau
Vietnam National University
$ xmms 'Tom Lehrer - Poisoning Pigeons in the Park.mp3'
$ rm 'b*lls and whistles'
\
in front of any single character to turn off its special meaning:$ echo M\&S
$ xmms Suzanne\ Vega\ -\ Tom\'s\ Diner.mp3
$ mail -s C:\\MSDOS.SYS windows-user@example.com
$
retains its special interpretation“
!
can't be escaped in double quotes$
, '
and \
:$ mail -s "C:\\MSDOS.SYS" windows-user@example.com
$ echo "It cost $price US\$"
$ echo "\*/"
\*/
You can build up an argument for a command by combining several chunks of differently-quoted text
Just put the chunks next to each other with no intervening whitespace:
$ echo "double-quoted"'.single-quoted.'unquoted
double-quoted.single-quoted.unquoted
$ echo 'She said, "Don'\''t go."'
She said, "Don't go."
$ echo "She said, \"Don't go.\""
*
in a glob pattern can stand for any sequence of characters:$ ls -l *.txt
-rw-rw-r-1 fred users 108 Nov 16 13:06 report.txt
-rw-rw-r-1 fred users 345 Jan 18 08:56 notes.txt
*
on its own expands to all files in the current directory$ ls Accounts/199*.txt
Accounts/1997.txt Accounts/1998.txt Accounts/1999.txt
$ ls ../images/*.gif
../images/logo.gif ../images/emblem.gif
$ cd /usr/man && ls man?/lp*
man1/lpq.1.gz man1/lprm.1.gz man4/lp.4.gz man8/lpc.8.gz
man1/lpr.1.gz man1/lptest.1.gz man8/lpd.8.gz
*
matches any sequence of characters?
:$ ls ?ouse.txt
Matches mouse.txt
and house.txt
, but not grouse.txt
$ rm ???*.txt
Matches any file ending in .txt that has at least three characters before the dot
Instead of matching any single character, we can arrange to match any of a given group of characters
*.[ch]
matches any file ending in .c
or .h
*[0-9].txt
matches any text file with a digit before the dot
You can use a caret as the first thing in the brackets to match any character that isn't one of the listed ones
[ˆa-z]*.jpg
matches any JPEG file that doesn't begin with a lower-case letter
To match any hidden file except the .
and ..
directories: .[ˆ.]*
{}
$ mkdir -p Accounts/200{1,2}
$ mkdir Accounts/200{1,2}/{0{1,2,3,4,5,6,7,8,9},1{0,1,2}}
$ mkdir -p Accounts/200{1,2}/{0{1,2,3,4,5,6,7,8,9},1{0,1,2}}
$ echo 'Hello '{world,Mum}\!
Hello world! Hello Mum!
for
), and even shell functionsThe Unix component approach makes it very easy to write shell scripts to perform fairly complex tasks
Common application domains for shell scripting include:
*** SALE $$$ ***.
.[ˆ.]*
to list all the hidden files in your home directory./bin
whose names end in sh
.[]
brackets to list all the files in /usr/bin
with names starting with a
, b
or c
.