5 ms·
For me, the most beautiful representation of a loop is like this: for((x=1;x<=5;x++)) do if [[ -v x ]] then echo $x fi done
by zazaulola 4y ago
For me, the most beautiful representation of a loop is like this:
for((x=1;x<=5;x++))
do if [[ -v x ]]
then echo $x
fi
done
- hddqsb 4y agoTip: You can use { } instead of do/done to emulate C syntax. Aside: [[ -v x ]] tests if the variable x is set. It should probably be [[ x -ne 0 ]] to match the example from the parent.
- cassepipe 4y ago"You can use { } instead of do/done" In sh ? In bash ? In zsh ? I am very glad if it is so in any of them
- hddqsb 4y agoIt works in bash/zsh/ksh but not in sh/dash. Examples: for i in 1 2 3 4 5; { echo $i; } for ((i=1;i<=5;i++)) { echo $i; } (It doesn't work with `while` loops, at least not in bash.)
- cardanome 4y agoI strongly dislike this style. Way too easy to commit a one off error. Personally, I never use naked loops if I can avoid them. Either foreach or higher order functions if the language has them.
- lambdaba 4y agoif you like pipes and a more functional style: seq 5 | xargs -n1 <...>