8 Command-line argument preprocessing Flashcards
Brace expansion
- in bash, not needed for posix
– Distributive law for string expansion
Provides for convenient entry of words with repeated substrings:
$ echo a{b,c,d}e
abe ace ade
$ echo {mgk25,fapp2,rja14}@cam.ac.uk
mgk25@cam.ac.uk fapp2@cam.ac.uk rja14@cam.ac.uk
$ rm slides.{bak,aux,dvi,log,ps}
Tilde expansion
Provides convenient entry of home directory pathname:
$ echo ~pb ~/Mail/inbox
/home/pb [username = pb]
[if only use tilde on its own – replace with own home directory]
/homes/mgk25/Mail/inbox
Parameter and command expansion - shell variable substitution
$ OBJFILE=skipjack.o $ echo ${OBJFILE} ${OBJFILE%.o}.c skipjack.o skipjack.c $ echo ${HOME} ${PATH} ${LOGNAME} /homes/mgk25 /bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin mgk25
Parameter and command expansion – with the standard output lines of commands
$ which emacs /usr/bin/emacs $ echo $(which emacs) /usr/bin/emacs $ ls -l $(which emacs)
Shorter notations for command and parameter expansion
variables without braces
$OBJFILE
and command substitution with grave accent (`)
echo which emacs
Pathname expansion
- Get access to fname without having to type full fname
- ? stands for an arbitrary single character
- stands for an arbitrary sequence of zero or more characters
- [. . . ] stands for one character out of a specified set. Use “-” to specify range of characters and “!” to complement set. Certain character classes can be named within [:. . . :].
None of the above will match a dot at the start of a filename, which is the naming convention for hidden files.
*.bak
[A-Za-z]*.???
[[:alpha:]]*
[!A-Z] .??*
files//.o
rm -rf .* - recursively delete, delete the .. directory
Quote removal
Three quotation mechanisms are available to enter the special characters in command-line arguments without triggering the corresponding shell substitution
suppresses all special character meanings
‘…’
except for ‘ itself
suppresses all special character meanings, except for $ \ `
“…”
suppresses including space character
\ - allows insertion of special characters, and suppress special meaning of just the next char
$ – command and variable substitution
` – command substitution
suppresses all special character meanings for the immediately
following character
\
e.g. of quote removal
$ echo
’$$$’
”* * * $HOME * * *”
$HOME
$$$ * * * /homes/mgk25 * * * $HOME
full C string quoting syntax in bash
The bash extension $’…’ provides access to the full C string quoting syntax. For example $’\x1b’ is the ASCII ESC character.