Differences

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

Link to this comparison view

Both sides previous revision Previous revision
syntax:expansion:brace [2014/04/27 16:16]
ormaaj [Repeating arguments or words] For the bored.
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>​ 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.+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**.
  • syntax/expansion/brace.1398615392.txt
  • Last modified: 2014/04/27 16:16
  • by ormaaj