A bash loop with braces? -
i stumbled across oddity , can't quite figure out why works:
$ ((i=0;i<8;i++)) { > printf '%d...' "$i"; > }; echo 0...1...2...3...4...5...6...7...
a for
loop in bash using brace syntax? can't reproduce using non-arithmetic for
loop @ all, , manpage , help
both seem require do
:
$ <snip> ((: (( exp1; exp2; exp3 )); commands; done arithmetic loop. equivalent (( exp1 )) while (( exp2 )); commands (( exp3 )) done
hmm, if just…
$ ((i=0)); while ((i<8)) { printf '%d...' "$i"; ((i++)); } -bash: syntax error near unexpected token `{'
nope!
is braced for-loop
syntax
- documented anywhere (official?)
- a legitimate different way represent arithmetic loop in bash?
- just i'm missing how "normal" arithmetic loop interpreting input?
p.s. – tested on os x mavericks both homebrew-installed bash 4.2.45(2)-release vanilla 3.2.51(1)-release.
transferring comments answer:
afaik, largely undocumented hack in bash, needed compatibility bourne shell supported brace notation, undocumented. not peculiar mac os x; works on ubuntu 12.04 too. doesn't seem work while
loops or until
loops in bash, though.
how write arithmetic loop in bourne shell syntax?
it optional way of writing body of for
loop; condition written differently in bourne shell, write things
for in 1 2 3 { echo $i }
in bourne shell, or use do
in place of {
, done
in place of }
.
(i'd forgotten existed until question reminded me of it; i've never used because not documented , hence not guaranteed supported.)
the do ... done
construct cannot used { ... }
can used; can use do ... done
after for
, while
or until
loop, whereas can write { echo hi; }
anywhere command appear (e.g. if { echo hi; }; echo lo; fi
. can't do
, done
.
the best way check veracity of answer to download heirloom bourne shell heirloom project.
i've downloaded source code heirloom bourne shell (a cvs repository) , built it, , tested it. indeed accept braces after loop, , refuses recognize them surrogates do
, done
after until
or while
loop.
Comments
Post a Comment