diff --git a/SC2051.md b/SC2051.md index 063c87e..0a2d417 100644 --- a/SC2051.md +++ b/SC2051.md @@ -24,18 +24,25 @@ In Bash, brace expansion happens before variable expansion. This means that brac For integers, use an arithmetic for loop instead. For zero-padded numbers or letters, use of eval may be warranted: +```bash from="a" to="m" for c in $(eval "echo {$from..$to}"); do echo "$c"; done +``` or more carefully (if `from`/`to` could be user input, or if the brace expansion could have spaces): +```bash from="a" to="m" while IFS= read -d '' -r c do echo "Read $c" done < <(eval "printf '%s\0' $(printf "{%q..%q}.jpg" "$from" "$to")") - +``` ### Exceptions None (if you're writing for e.g. zsh, make sure the shebang indicates this so shellcheck won't warn) + +### Related Resources: + +* [StackOverflow: Variables in bash seq replacement ({1..10})](https://stackoverflow.com/questions/169511/how-do-i-iterate-over-a-range-of-numbers-defined-by-variables-in-bash) \ No newline at end of file