shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

SC2086 on already quoted nested command

Open MrKeiKun opened this issue 1 year ago • 2 comments

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

MrKeiKun avatar Jan 15 '24 02:01 MrKeiKun

it seems like $path must double quoted as well to fixed issue with directory with spaces. but $findArg still gives warning regarding to it.

MrKeiKun avatar Jan 15 '24 08:01 MrKeiKun

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.

Ordoviz avatar Jan 22 '24 14:01 Ordoviz

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 avatar Apr 05 '24 18:04 bhundven

@bhundven : You need to quote the variable in the inner shell as well.

"$(dirname "${image}")"

brother avatar Apr 09 '24 22:04 brother

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.

koalaman avatar Apr 14 '24 23:04 koalaman