Zhou haihan/Wen
2010.6.10
BASH Shell extension refers to an operation in which Shell expands commands when analyzing input commands. There are 7 extension methods. Refer to the man bash extension page:
Expansion <br/> the command line extension is implemented after being split into words. There are seven types of extensions: Brace expansion (Curly extension), Tilde expan-<br/> Sion (wavy extension), parameter and Variable Expansion (parameter and variable extension ), command substi-<br/> tution (command replacement), arithmetic expansion (arithmetic extension), word splitting (word splitting ), and path-<br/> name expansion (Path Extension ). <br/> the extended sequence is: Brace expansion, Tilde expansion, parameter, variable, and Arith-<br/> metic expansion and command substitution (in the left-to-right order ), word splitting, <br/> pathname Expansion. <br/> There is also an additional extension: Process subtitution (process replacement) is only valid in systems that support it. <Br/> only brace expansion, word splitting, and pathname expansion <br/> the number of words before and after expansion changes. Other extensions always extend a word into one word. The only exception is the "$ @" and "$ {name [@]}" mentioned above (see <br/> parameters ). <Br/> brace expansion is a mechanism that may generate arbitrary strings. This mechanism is similar to pathname expansion. <br/> but the corresponding file does not need to exist. The extension mode of curly braces is an optional preamble (Leading Character). <br/> strings separated by commas (,) are included in a pair of curly braces, next is an optional postscript (postscript ). <Br/> the prefix is added to the front of each string in curly braces. The postscript is appended to each result string and expanded from left to right. <Br/> curly braces can be nested. The results of the extended string are not sorted, but the order from left to right is retained. For example, a {D, C, B} e is extended to 'ade ace AB '. <Br/> the curly braces extension is performed before any other extension, and any characters that have special significance for other extensions are retained in the result. It is strictly literal. Bash <br/> does not provide any semantic explanation for the expanded context or text in curly brackets. <Br/> the correct curly braces extension must contain left and right brackets that are not referenced, and at least one comma that is not referenced. Any incorrect expression will not be changed. You can use a backslash to reference {or, <br/> to block them from being recognized as part of the curly brackets expression. To avoid conflict with parameter extensions, strings $ {are not considered a valid combination. <Br/> this structure is usually used to abbreviated strings with a public prefix that is far longer than in the preceding example. For example: <br/> mkdir/usr/local/src/bash/{old, new, DIST, bugs} <br/> or: <br/> chown root/usr/{UCB/{ex, edit}, lib/{ex ?.? *, How_ex }}< br/> curly extension causes a slight incompatibility with the previous version of sh. When the left or right parentheses appear as part of the word, SH does not specially process them and retains them in the output. <Br/> bash removes parentheses from the words in the expanded result of curly brackets. For example, inputting file {1, 2} into sh will cause constant output. After the same input is expanded in bash <br/>, file1 file2 is output. to ensure strict compatibility with SH, you must use the + B option when starting bash, alternatively, use the <br/> set command and the + B Option to disable the curly braces extension (see the shell built-in command (shell builtin commands) section below ). <Br/>
The braces (curly braces) extension method is also interesting.
Example 1: operate a file with multiple file names in common
Zhouhh @ zhh64 :~ /Brace $ ls
BK
Zhouhh @ zhh64 :~ /Brace $ touch file1_source,target,info,readme=.txt
Zhouhh @ zhh64 :~ /Brace $ ls
BK fileinfo.txt filereadme.txt filesource.txt filetarget.txt
Zhouhh @ zhh64 :~ /Brace $ echo filepath, 42.16.txt
File1.txt file2.txt file3.txt file4.txt
Zhouhh @ zhh64 :~ /Brace $ touch filepath, 42.16.txt
Zhouhh @ zhh64 :~ /Brace $ ls
BK file1.txt file2.txt file3.txt file4.txt
Example 2: A command transfers several files with regular file names to a directory
Zhouhh @ zhh64 :~ /Brace $ ls BK
Zhouhh @ zhh64 :~ /Brace $ MV filepath, 42.16.txt BK /.
Zhouhh @ zhh64 :~ /Brace $ ls BK
File1.txt file2.txt file3.txt file4.txt
Zhouhh @ zhh64 :~ /Brace $
Example 3: Backup
Zhouhh @ zhh64 :~ /Brace $ touch mydata. Log
Zhouhh @ zhh64 :~ /Brace $ VI back. Sh
Zhouhh @ zhh64 :~ /Brace $ chmod + X back. Sh
Zhouhh @ zhh64 :~ /Brace $ cat back. Sh
Set-X # debug mode
Bkdate = 'date + % F' # date
CP mydata {, $ bkdate}. log # Back up log to date format
Zhouhh @ zhh64 :~ /Brace $ ls
Back. Sh BK mydata. Log
Zhouhh @ zhh64 :~ /Brace $./back. Sh
+++ Date + % F
+ Bkdate = 2010-06-10
+ CP mydata. Log mydata2010-06-10.log
Zhouhh @ zhh64 :~ /Brace $ ls
Back. Sh BK mydata2010-06-10.log mydata. Log
Example 4 process multiple regular file names
Zhouhh @ zhh64 :~ /Brace $ echo/var/log/messages. {1 .. 3}
/Var/log/messages.1/var/log/messages.2/var/log/messages.3
Zhouhh @ zhh64 :~ /Brace $ echo file=a..d=255.1..32.16.txt
Filea1.txt filea2.txt filea3.txt fileb1.txt fileb2.txt fileb3.txt filec1.txt filec2.txt filec3.txt filed1.txt filed2.txt filed3.txt
Example 5 for Loop
Zhouhh @ zhh64 :~ /Brace $ for I in {1... 9... 3}
> Do
> Echo $ I
> Done
1
4
7
Refer:
Http://www.thegeekstuff.com/2010/06/bash-shell-brace-expansion/#more-4614