Monday, February 26, 2024
rated 0 times [  5] [ 0]  / answers: 1 / hits: 422  / 2 Years ago, sat, april 30, 2022, 12:13:05

I have a problem with my shell script and The following script works like a charm, but shellcheck tells me I have to quote variables, but quoting variables in the script stops the script from working (I need word splitting).

Can someone help me, please?

apt-get $COMMAND -y -qq $PACKAGES >/dev/null &
^-- SC2086 (info): Double quote to prevent globbing and word splitting.
^-- SC2086 (info): Double quote to prevent globbing and word splitting.

More From » command-line


At a guess - because you really haven't included enough information about the problem - you are doing something like

PACKAGES='foo bar baz'

and then when you double-quote the variable like

apt-get "$COMMAND" -y -qq "$PACKAGES"

the apt-get command fails with an error like E: Unable to locate package foo bar baz. In other words, you're relying on the shell's default word-splitting behavior to turn the unquoted expansion $PACKAGES into individual tokens.

The right way to fix that in bash is to use an array variable instead:

PACKAGES=(foo bar baz)

apt-get "$COMMAND" -y -qq "${PACKAGES[@]}"

See also:

I'd also suggest avoinding ALLCAPS variable names - but that's a battle for another day

[#14] Sunday, May 1, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.

Total Points: 412
Total Questions: 105
Total Answers: 118

Location: Trinidad and Tobago
Member since Fri, May 8, 2020
4 Years ago