From db97acd01d00afc770970b4e0d59568a1f437a64 Mon Sep 17 00:00:00 2001 From: koalaman Date: Fri, 8 Aug 2014 17:18:10 -0700 Subject: [PATCH] Created SC2043 (markdown) --- SC2043.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 SC2043.md diff --git a/SC2043.md b/SC2043.md new file mode 100644 index 0000000..e214cbf --- /dev/null +++ b/SC2043.md @@ -0,0 +1,38 @@ +## This loop will only run once, with var=value + +### Problematic code: + + for var in value + do + echo "$var" + done + +### Correct code: + +Correct code depends on what you want to do. + +To iterate over files in a directory, instead of `for var in /my/dir` use: + + for var in /my/dir/* ; do echo "$var"; done + +To iterate over lines in a file or command output, use a while read loop instead: + + mycommand | while IFS= read -r line; do echo "$line"; done + +To iterate over *words* written to a command or function's stdout, instead of `for var in myfunction`, use + + for var in $(myfunction); do echo "$var"; done + +To iterate over *words* in a variable, instead of `for var in myvariable`, use + + for var in $myvariable; do echo "$var"; done + + + +### Rationale: + +ShellCheck has detected that your for loop iterates over a single, constant value. This is most likely a bug in your code, caused by you not expanding the value in the way you want. + +### Contraindications + +None. \ No newline at end of file