mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created Sc2046 (markdown)
32
Sc2046.md
Normal file
32
Sc2046.md
Normal file
@@ -0,0 +1,32 @@
|
||||
# Quote this to prevent word splitting
|
||||
|
||||
### Problematic code:
|
||||
|
||||
ls -l $(getfilename)
|
||||
|
||||
### Correct code:
|
||||
|
||||
# getfilename outputs 1 file
|
||||
ls -l "$(getfilename)"
|
||||
|
||||
# getfilename outputs multiple files, linefeed separated
|
||||
getfilename | while IFS='' read -r line
|
||||
do
|
||||
ls -l "$line"
|
||||
done
|
||||
|
||||
### Rationale:
|
||||
|
||||
When command expansions are unquoted, word splitting and globbing will occur. This often manifests itself by breaking when filenames contain spaces.
|
||||
|
||||
Trying to fix it by adding quotes or escapes to the data will not work. Instead, quote the command substitution itself.
|
||||
|
||||
If the command substitution outputs multiple pieces of data, use a loop instead.
|
||||
|
||||
### Contraindications
|
||||
|
||||
In rare cases you actually want word splitting, such as in
|
||||
|
||||
gcc $(pkg-config --libs openssl) client.c
|
||||
|
||||
This is because `pkg-config` outputs `-lssl -lcrypto`, which you want to break up by spaces into `-lssl` and `-lcrypto`.
|
Reference in New Issue
Block a user