Shell Expansion

Pathname Expansion:

[me@linuxbox me]$ echo [[:upper:]]*
Desktop Documents Music Pictures Public Templates Videos

Tilde Expansion:

[me@linuxbox me]$ echo ~
/home/me

Arithmetic Expansion:

[me@linuxbox me]$ echo $((2 + 2))
4

which takes the form: 

$((expression))

Arithmetic expansion only supports integers (whole numbers, no decimals).

Braces Expansion:

Maybe this is the strangest expansion. you can create multiple strings with a pattern of braces. such as:

[me@linuxbox me]$ echo Front-{A,B,C}-Back
Front-A-Back Front-B-Back Front-C-Back

The content in the braces may contain a preamble and a postscript.

[me@linuxbox me]$ echo Number_{1..5}
Number_1 Number_2 Number_3 Number_4 Number_5

It can also be nested:

[me@linuxbox me]$ echo a{A{1,2},B{3,4}}b
aA1b aA2b aB3b aB4b

So what is this good for? Consider you have a bunch of folders to be created:

[me@linuxbox me]$ mkdir Photos
[me@linuxbox me]$ cd Photos
[me@linuxbox Photos]$ mkdir {2007..2009}-0{1..9} {2007..2009}-{10..12}
[me@linuxbox Photos]$ ls

2007-01 2007-07 2008-01 2008-07 2009-01 2009-07
2007-02 2007-08 2008-02 2008-08 2009-02 2009-08
2007-03 2007-09 2008-03 2008-09 2009-03 2009-09
2007-04 2007-10 2008-04 2008-10 2009-04 2009-10
2007-05 2007-11 2008-05 2008-11 2009-05 2009-11
2007-06 2007-12 2008-06 2008-12 2009-06 2009-12

Parameter Expansion:

[me@linuxbox me]$ echo $USER
me

To see available variables:

[me@linuxbox me]$ printenv | less

Command Substituion

Command Substitution allows us to use the output of a command as an expansion. e.g.

[me@linuxbox me]$ echo $(ls)
Desktop Documents ls-output.txt Music Pictures Public Templates Videos

Another one:

me@linuxbox me]$ ls -l $(which cp)
-rwxr-xr-x 1 root root 71516 2007-12-05 08:58 /bin/cp

The alternative syntax is to use back-quotes instead of the dollar sign and parenthesis:

[me@linuxbox me]$ ls -l `which cp`
-rwxr-xr-x 1 root root 71516 2007-12-05 08:58 /bin/cp

Double Quotes:

All special characters used by the shell lose their special meaning and are treated as ordinary characters. The exceptions are "$", "" and "`". This means word-splitting, pathname expansion, tilde expansion, and brace expansion are all suppressed. but parameter expansion, arithmetic expansion, ad command substitution are still carried out.

[me@linuxbox me]$ echo "$USER $((2+2)) $(cal)"

me 4
February 2008
Su Mo Tu We Th Fr Sa
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29

The fact that newlines are considered delimiters by the word-splitting mechanism causes an interesting, albeit subtle, effect on command substitution. Consider the followings:

[me@linuxbox me]$ echo $(cal)
February 2008 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
[me@linuxbox me]$ echo "$(cal)"

February 2008
Su Mo Tu We Th Fr Sa
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29

Single Quotes

To suppress all expansions, use single quotes:

[me@linuxbox me]$ echo text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER
text /home/me/ls-output.txt a b foo 4 me
[me@linuxbox me]$ echo "text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER"
text ~/*.txt {a,b} foo 4 me
[me@linuxbox me]$ echo 'text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER'
text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER

Escaping Characters:

Backslash is used as the escape character. It's usually used to selectively prevent an expansion:

[me@linuxbox me]$ echo "The balance for user $USER is: $5.00"
The balance for user me is: $5.00

More about backslash

Use backslash to get the shell to ignore the newline like this:

ls -l 
   --reverse 
   --human-readable 
   --full-time
       

Backslash escape characters:

[me@linuxbox me]$ echo -e "Inserting several blank lines


"
Inserting several blank lines



[me@linuxbox me]$ echo -e "Words	separated	by	horizontal	tabs."
Words separated   by  horizontal  tabs
[me@linuxbox me]$ echo -e "aMy computer went "beep"."

My computer went "beep".

[me@linuxbox me]$ echo -e "DEL C:\WIN2K\LEGACY_OS.EXE"

DEL C:WIN2KLEGACY_OS.EXE

From: http://linuxcommand.org/lc3_lts0080.php

原文地址:https://www.cnblogs.com/mush0m/p/3609046.html