From 6410432292ecb66969830d1854811bb20a03cd5d Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Fri, 23 Mar 2018 08:48:26 -0700 Subject: [PATCH] Created SC2231 (markdown) --- SC2231.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 SC2231.md diff --git a/SC2231.md b/SC2231.md new file mode 100644 index 0000000..68ed4dc --- /dev/null +++ b/SC2231.md @@ -0,0 +1,33 @@ +## Quote expansions in this for loop glob to prevent wordsplitting, e.g. "$dir"/*.txt . + +### Problematic code: + +```sh +for file in $dir/*.txt +do + echo "Found $file" +done +``` + +### Correct code: + +```sh +for file in "$dir"/*.txt +do + echo "Found $file" +done +``` + +### Rationale: + +When iterating over globs containing expansions, you can still quote all expansions in the path to better handle whitespace and special characters. + +Just make sure glob characters are outside quotes. `"$dir/*.txt"` will not glob expand, but `"$dir"/*.txt` or `"$dir"/*."$ext"` will. + +### Exceptions: + +Exceptions similar to [[SC2086]] apply. If the variable is expected to contain globs, such as if `dir="tmp/**"` in the example, you can ignore this message. + +### Related resources: + +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc! \ No newline at end of file