Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Bash (Unix shell)
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Brace Expansion === Brace expansion, also called alternation, is a feature copied from the [[C shell]]. It generates a set of alternative combinations. Generated results need not exist as files. The results of each expanded string are not sorted and left to right order is preserved: <syntaxhighlight lang="console"> $ echo a{p,c,d,b}e ape ace ade abe $ echo {a,b,c}{d,e,f} ad ae af bd be bf cd ce cf </syntaxhighlight> Users should not use brace expansions in portable shell scripts, because the [[Bourne shell]] does not produce the same output. <syntaxhighlight lang="console"> $ # bash shell $/bin/bash -c 'echo a{p,c,d,b}e' ape ace ade abe $ # A traditional shell does not produce the same output $ /bin/sh -c 'echo a{p,c,d,b}e' a{p,c,d,b}e </syntaxhighlight> When brace expansion is combined with wildcards, the braces are expanded first, and then the resulting wildcards are substituted normally. Hence, a listing of JPEG and PNG images in the current directory could be obtained using: <syntaxhighlight lang="bash"> ls *.{jpg,jpeg,png} # expands to *.jpg *.jpeg *.png - after which, # the wildcards are processed echo *.{png,jp{e,}g} # echo just shows the expansions - # and braces in braces are possible. </syntaxhighlight> In addition to alternation, brace expansion can be used for sequential ranges between two integers or characters separated by double dots. Newer versions of Bash allow a third integer to specify the increment. <syntaxhighlight lang="console"> $ echo {1..10} 1 2 3 4 5 6 7 8 9 10 $ echo {01..10} 01 02 03 04 05 06 07 08 09 10 $ echo file{1..4}.txt file1.txt file2.txt file3.txt file4.txt $ echo {a..e} a b c d e $ echo {1..10..3} 1 4 7 10 $ echo {a..j..3} a d g j </syntaxhighlight> When brace expansion is combined with variable expansion (A.K.A. ''parameter expansion'' and ''parameter substitution'') the variable expansion is performed ''after'' the brace expansion, which in some cases may necessitate the use of the <code>eval</code> built-in, thus: <syntaxhighlight lang="console"> $ start=1; end=10 $ echo {$start..$end} # fails to expand due to the evaluation order {1..10} $ eval echo {$start..$end} # variable expansion occurs then resulting string is evaluated 1 2 3 4 5 6 7 8 9 10 </syntaxhighlight> <!-- === Tilde Expansion === --><!-- === Parameter and Variable Expansion === '''+++ Attributes''' '''+++ Scalar Variables''' '''+++ Array Variables''' '''++++++ Indexed Arrays''' '''++++++ Associative Arrays''' '''+++ Shell Variables''' '''++++++ Positional Parameters''' '''++++++ Special Variables''' '''++++++ POSIX Variables''' '''++++++ Non-POSIX Variables''' '''+++ <code>export</code> and Environment Variables''' --><!-- === Command Substitution (again) === '''+++ Modern <code>$(...)</code>''' '''+++ Deprecated <code>`...`</code>''' --><!-- === Process Substitution (again) === '''+++ <code><(...)</code> and <code>>(...)</code>''' --><!-- === Arithmetic Expansion === '''+++ <code>((..))</code>''' '''+++ <code>$((...))</code>''' '''+++ Other Arithmetic Contexts''' '''+++ Integer Variables''' --><!-- === Redirections === '''+++ Standard''' '''+++ Here Documents''' '''+++ Here Strings''' --><!-- === Aliases === --><!-- === Reserved Words and Grammar === --><!-- === Functions === --><!-- === Built-Ins === --><!-- === Command Lookup === -->
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Bash (Unix shell)
(section)
Add topic