BASH:question about for i in {a..b}
Dear Experts,
With the code Code:
#!/bin/bash Code:
Welcome 1 times Code:
Welcome {1..5} times I would thank for your help. |
The problem is that the brace expansion is done before the parameter expansion.
Code:
$ for i in "Welcome "{1..5}" times.";do echo ${i};done As a work-around, this might help: Code:
#!/bin/bash Code:
$ bash tmp3 |
Well, I tend to use seq for for loops. In fact I don't use a lot of bash structures, because I have no need of them (and they are confusing and I don't remember them).
Code:
bash-4.1$ j=5;for i in $(seq 1 $j);do echo Welcome $i times.;done |
Quote:
Code:
max=5 |
I don't think you can do this unless you use the alternative forms posted by the others.
Quoting from http://www.gnu.org/software/bash/man...xpansion.html: "Brace expansion is performed before any other expansions, and any characters special to other expansions are preserved in the result." So $max is evaluated only after the brace expansion is done. You can see the difference here: Code:
$ bash -x |
This is common Bash Pitfall #33
http://mywiki.wooledge.org/BashPitfa..._.7B1...24n.7D Quite a few problems like this come down to the shell's parsing order. It really helps to clearly understand what happens before what when a command line is executed. I've often thought it would be nice though if bash could implement a shell option to enable variable expansion inside brace expansion. It would make some coding situations much easier. But I suppose it would probably be kind of tricky to implement internally, due to the need to account for parameter expansion braces. And nobody suggest using eval please, ok? Incidentally, another option for printing simple sequences like this is to use seq's (at least the gnu version's) format option. Code:
seq -f 'Welcome %g times.' "$n" |
All times are GMT -5. The time now is 12:22 PM. |