Tuesday, October 18, 2011

eval is your friend

I like to use expressions like {1..100} in bash - it makes tedious FORs much more easier. But you can't express this using variables. Even via readonly variables.So, you can not easily write this:

readonly START_ID=200
readonly END_ID=250

for i in {{1..100},{$START_ID..$END_ID}}; do
   # do something using $i

But (thank God) here is pretty workaround: eval. So, construction above may be rewritten as

for i in $( eval echo {{1..100},{$START_ID..$END_ID}} ); do
   # ...

No comments:

Post a Comment