PathnameExpansion
TildeExpansion~
When used at the beginning of a word, itexpands into the name of the home directory of the named user, orif no user is named, the home directory of the currentuser:
ArithmeticExpansion
$((expression)) $(((5**2)*3))
BraceExpansion
Withit, you can create multiple text strings from a pattern containingbraces. Here's anexample:
[me@linuxboxme]$echoFront-{A,B,C}-Back
Front-A-Back Front-B-BackFront-C-Back
Patterns to be brace expanded may containa leading portion calledapreambleand a trailingportion called apostscript. The braceexpression itself may contain either a comma-separated list ofstrings, or a range of integers or single characters. The patternmay not contain embedded whitespace. Here is an example using arange of integers:
[me@linuxboxme]$echoNumber_{1..5}
Number_1 Number_2 Number_3 Number_4Number_5
Arange of letters in reverse order:
[me@linuxboxme]$echo{Z..A}
Z Y X WV U T S R Q P O N M L K J I H G F E D C BA
Brace expansions may benested:
[me@linuxboxme]$echoa{A{1,2},B{3,4}}b
aA1baA2b aB3baB4b
Sowhat is this good for? The most commonapplication is to make lists of files or directories to be created.For example, if you were a photographer and had a largecollection of images you wanted to organize into years and months,the first thing you might do is create a series of directoriesnamed in numeric “Year-Month” format. This way, the directory nameswill sort in chronological order. You could type out a completelist of directories, but that's a lot of work and it's error-pronetoo. Instead, you could do this:
[me@linuxboxme]$mkdirPhotos
[me@linuxboxme]$cdPhotos
[me@linuxboxPhotos]$mkdir {2007..2009}-0{1..9}{2007..2009}-{10..12}
[me@linuxboxPhotos]$ls
2007-01 2007-07 2008-01 2008-07 2009-01 2009-072007-02 2007-08 2008-02 2008-08 2009-02 2009-082007-03 2007-09 2008-03 2008-09 2009-03 2009-092007-04 2007-10 2008-04 2008-10 2009-04 2009-102007-05 2007-11 2008-05 2008-11 2009-05 2009-112007-06 2007-12 2008-06 2008-12 2009-06 2009-12
ParameterExpansion
CommandSubstitution
Commandsubstitutionallows us to use the output of acommand as an expansion:
[me@linuxboxme]$echo $(ls)
DesktopDocuments ls-output.txt Music Pictures Public TemplatesVideos
One of my favorites goes something likethis:
[me@linuxboxme]$ls -l $(whichcp)
-rwxr-xr-x 1 root root 71516 2007-12-0508:58/bin/cp
Here we passed the results ofwhichcpas an argument tothelscommand, therebygetting the listing of ofthecpprogramwithout having to know its full pathname. We are not limited tojust simple commands. Entire pipelines can be used (only partialoutput shown):
[me@linuxboxme]$file $(ls /usr/bin/* | grepbin/zip)
In thisexample, the results of the pipeline became the argument list ofthe file command. There is an alternate syntax for commandsubstitution in older shell programs which is also supportedinbash. Ituses back-quotes instead of the dollar sign andparentheses:[me@linuxboxme]$ls -l `whichcp`
-rwxr-xr-x 1 root root 71516 2007-12-0508:58/bin/cp
DoubleQuotes
If youplace text inside double quotes, all the special characters used bythe shell lose their special meaning and are treated as ordinarycharacters. The exceptions are “$”, “”(backslash), and “`” (back- quote). This means thatword-splitting, pathname expansion, tilde expansion, and braceexpansion are suppressed, but parameter expansion, arithmeticexpansion, and command substitution are still carriedout.
Oncethe double quotes are added, our command line contains a commandfollowed by a single argument. The fact that newlines areconsidered delimiters by the word-splitting mechanism causes aninteresting, albeit subtle, effect on command substitution.Consider thefollowing:
[me@linuxboxme]$echo$(cal)
February 2008 Su Mo Tu We Th Fr Sa 1 2 34 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 272829
[me@linuxboxme]$echo"$(cal)"
February 2008
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 910 11 12 13 14
15 1617 18 19 20 21
22 2324 25 26 27 28
29
Inthe first instance, the unquoted command substitution resulted in acommand line containing thirty-eight arguments. In the second,a command line with one argument thatincludes the embedded spaces andnewlines.
SingleQuotes
Ifyou need to suppress all expansions, you use single quotes. Here isa comparison of unquoted, double quotes, and singlequotes:
[me@linuxboxme]$echo text ~/*.txt {a,b} $(echo foo)$((2+2))$USER
text/home/me/ls-output.txt a b foo 4me
[me@linuxboxme]$echo "text ~/*.txt {a,b} $(echo foo)$((2+2))$USER"
text~/*.txt {a,b} foo 4me
[me@linuxboxme]$echo 'text ~/*.txt {a,b} $(echo foo)$((2+2))$USER'
text~/*.txt {a,b} $(echo foo) $((2+2))$USER
Asyou can see, with each succeeding level of quoting, more and moreof the expansions are suppressed.
EscapingCharacters
Sometimes you only want to quote a singlecharacter. To do this, you can precede a character with abackslash, which in this context is calledtheescapecharacter.
If youlook atthemanpages for any programwritten bytheGNUproject, youwill notice that in addition to command line options consisting ofa dash and a single letter, there are also long option names thatbegin with two dashes. For example, the following areequivalent:
ls -rls --reverse
Why do they support both? The short formis for lazy typists on the command line and the long form is mostlyfor scripts though some options may only be long form. I sometimesuse obscure options, and I find the long form useful if I have toreview a script again months after I wrote it. Seeing the long form helps me understand what theoption does, saving me a trip tothemanpage. A little moretyping now, a lot less work later. Laziness ismaintained.
Asyou might suspect, using the long form options can make a singlecommand line very long. To combat thisproblem, you can use a backslash to get the shell to ignore anewline character likethis:
ls -l --reverse --human-readable --full-time
Using the backslash in this way allows usto embed newlines in our command. Note that for this trick to work,the newline must be typed immediately after the backslash. If youput a space after the backslash, the space will be ignored, not thenewline. Backslashes are also used to insert special charactersinto our text. These are calledbackslashescape characters. Here are the commonones: