From d7a155e77dccf9ecfaa83389ce2d82335aad547a Mon Sep 17 00:00:00 2001 From: koalaman Date: Thu, 25 Jan 2018 19:29:07 -0800 Subject: [PATCH] Updated SC2051 (markdown) --- SC2051.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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