shellcheck
shellcheck copied to clipboard
SC2086 on already quoted nested command
For bugs
- Rule Id : SC2086
- My shellcheck version: v0.35.0 VS Code
- [x] The rule's wiki page does not already cover this (e.g. https://shellcheck.net/wiki/SC2086)
- [x] I tried on https://www.shellcheck.net/ and verified that this is still a problem on the latest commit
Here's a snippet or screenshot that shows the problem:
#!/bin/bash
path="some/path"
findArg="! -name 20240101"
readarray -t toBeDeleted <<< "$(find $path -maxdepth 1 -type d $findArg | grep -E '([0-9]{8})' | sed 's\^./\\g' | grep -o "[^/]*$" | sort)"
for k in "${toBeDeleted[@]}"
do
echo "${path:?}/$k"
rm -rfv "${path:?}/$k"
done
Here's what shellcheck currently says:
it says $findArg must be Double quote to prevent globbing and word splitting.
Here's what I wanted or expected to see:
There should be no error
it seems like $path must double quoted as well to fixed issue with directory with spaces. but $findArg still gives warning regarding to it.
Shellcheck is right unless you want * and other globs in findArg be replaced by filenames. Since you are using Bash, make findArg an array and replace $findArg with "${findArg[@]}" as suggested on StackExchange.
I get similar with:
images="$(find . -name 'Dockerfile*' -type f)"
for image in "${images[@]}"; do
I image_dir="$(dirname ${image})" # I: Double quote to prevent globbing and word splitting.
echo "${image_dir}"
done
${images} is already double quoted inside of "$(dirname ${images}".
@bhundven : You need to quote the variable in the inner shell as well.
"$(dirname "${image}")"
ShellCheck is right, the variables in question must indeed be separately quoted. The wiki explicitly mentions this case.
If it worked as described you could simply have put echo "$( as the first line and )" as the last line in a file in order to have the whole script run with correct quoting, but alas.