Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
syntax:expansion:brace [2010/08/25 06:06]
127.0.0.1 external edit
syntax:expansion:brace [2014/09/03 09:56] (current)
glorieul Added an example (probably obvious for most people, but not to rookies like myself)
Line 28: Line 28:
 echo a$PATH b$PATH echo a$PATH b$PATH
 </​code>​ </​code>​
-Another common pitfall is to assume that a range like ''​{1..200}''​ can be expressed with variables using ''​{$a..$b}''​. Due to what I described above, it **simply is not possible**, because it's the very first step in doing expansions. A possible way to achieve this, if you really can't handle this in another way, is using the ''​eval''​ command, which basically evaluates a commandline twice: <​code>​eval echo {$a..$b}</​code>​ +Another common pitfall is to assume that a range like ''​{1..200}''​ can be expressed with variables using ''​{$a..$b}''​. Due to what I described above, it **simply is not possible**, because it's the very first step in doing expansions. A possible way to achieve this, if you really can't handle this in another way, is using the ''​eval''​ command, which basically evaluates a commandline twice: <​code>​eval echo {$a..$b}</​code> ​For instance, when embedded inside a for loop : <​code>​for i in $(eval echo {$a..$b})</​code>​ This requires that the entire command be properly escaped to avoid unexpected expansions. If the sequence expansion is to be assigned to an array, another method is possible using declaration commands: <​code>​declare -a '​pics=(img{'"​$a..$b"'​}.png)';​ mv "​${pics[@]}"​ ../​imgs</​code>​ This is significantly safer, but one must still be careful to control the values of $a and $b. Both the exact quoting, and explicitly including "​-a"​ are important.
  
 The brace expansion is present in two basic forms, **string lists** and **ranges**. The brace expansion is present in two basic forms, **string lists** and **ranges**.
Line 68: Line 67:
  
 Brace expansion using ranges is written giving the startpoint and the endpoint of the range. This is a "​sequence expression"​. The sequences can be of two types Brace expansion using ranges is written giving the startpoint and the endpoint of the range. This is a "​sequence expression"​. The sequences can be of two types
-  * integers (:V4: optionally zero padded, optionally with a given increment)+  * integers (optionally zero padded, optionally with a given increment)
   * characters   * characters
  
Line 85: Line 84:
 </​code>​ </​code>​
  
-:V4: When you zeropad one of the numbers (or both) in a range, then the generated range is zeropadded, too:+When you zeropad one of the numbers (or both) in a range, then the generated range is zeropadded, too:
 <​code>​ <​code>​
 $ echo {01..10} $ echo {01..10}
 01 02 03 04 05 06 07 08 09 10 01 02 03 04 05 06 07 08 09 10
 </​code>​ </​code>​
-:V4: There'​s a chapter of Bash 4 brace expansion changes at [[#​new_in_bash_4.0 | the end of this article]].+There'​s a chapter of Bash 4 brace expansion changes at [[#​new_in_bash_4.0 | the end of this article]].
  
 Similar to the expansion using stringlists,​ you can add preamble and postscript strings: Similar to the expansion using stringlists,​ you can add preamble and postscript strings:
Line 170: Line 169:
 </​code>​ </​code>​
  
-:V4: See the [[#​news_in_bash_4.0 | text below]] for a new Bash 4 method.+See the [[#​news_in_bash_4.0 | text below]] for a new Bash 4 method.
  
 ==== Repeating arguments or words ==== ==== Repeating arguments or words ====
Line 184: Line 183:
 ...which is a kind of a hack, but hey, it works. ...which is a kind of a hack, but hey, it works.
  
-===== New in Bash 4.0 =====+<div round info> 
 +=== More fun ==
 +The most optimal possible brace expansion to expand n arguments of course consists of n's prime factors. We can use the "​factor"​ program bundled with GNU coreutils to emit a brace expansion that will expand any number of arguments.
  
-:V4:+<​code>​ 
 +function braceify { 
 +    [[ $1 == +([[:digit:]]) ]] || return 
 +    typeset -a a 
 +    read -ra a < <(factor "​$1"​) 
 +    eval "echo $(printf '​{$(printf ,%%.s {1..%s})}'​ "​${a[@]:​1}"​)"​ 
 +
 + 
 +printf 'eval printf "​$arg"​%s'​ "​$(braceify 1000000)"​ 
 +</​code>​ 
 + 
 +"​Braceify"​ generates the expansion code itself. In this example we inject that output into a template which displays the most terse brace expansion code that would expand ''"​$arg"''​ 1,000,000 times if evaluated. In this case, the output is: 
 + 
 +<​code>​ 
 +eval printf "​$arg"​{,,​}{,,​}{,,​}{,,​}{,,​}{,,​}{,,,,,​}{,,,,,​}{,,,,,​}{,,,,,​}{,,,,,​}{,,,,,​} 
 +</​code>​ 
 +</​div>​ 
 +===== New in Bash 4.0 =====
  
 ==== Zero padded number expansion ==== ==== Zero padded number expansion ====
  • syntax/expansion/brace.1282716406.txt
  • Last modified: 2012/03/08 04:26
  • (external edit)