Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Bash (Unix shell)
(section)
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 === -->
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)