shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

SC2086: unnecessary quoting for `$_`

Open e-kwsm opened this issue 2 years ago • 3 comments

For bugs

  • Rule Id (if any, e.g. SC1000): SC2086
  • My shellcheck version (shellcheck --version or "online"): 0.8.0 and online
  • [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

For new checks and feature suggestions

  • [ ] https://www.shellcheck.net/ (i.e. the latest commit) currently gives no useful warnings about this
  • [ ] I searched through https://github.com/koalaman/shellcheck/issues and didn't find anything related

Here's a snippet or screenshot that shows the problem:

#!/bin/bash
foo=bar
echo $foo

: bar
echo $_

Here's what shellcheck currently says:


In myscript line 6:
echo $_
     ^-- SC2086 (info): Double quote to prevent globbing and word splitting.

Did you mean: 
echo "$_"

For more information:
  https://www.shellcheck.net/wiki/SC2086 -- Double quote to prevent globbing ...

Here's what I wanted or expected to see:

SC2086 is not issued when the last argument, $_, is so simple that quoting is not necessary.

e-kwsm avatar Aug 28 '23 06:08 e-kwsm

Here's an example where word splitting could be an issue. When the input to printf -v is quoted, word splitting is not performed and the value of $e matches the original value of $a .

$ a='b c d' $ echo "$a" b c d $ printf -v e $_ $ declare -p e declare -- e="b" $ echo "$a" b c d $ printf -v e "$_" $ echo "$a" b c d

Underscore's, or any variable's, (debateable) "simplicity" doesn't have any effect either way on whether the shell performs word splitting while expanding said variables.

wileyhy avatar Aug 29 '23 19:08 wileyhy

I do not know in detail how shellcheck evaluates simplicity of a variable, but SC2086 is not issued to foo=bar; echo $foo. How about : bar; echo $_?

e-kwsm avatar Sep 03 '23 16:09 e-kwsm

That's interesting.

On Sun, Sep 3, 2023, 9:13 AM Eisuke Kawashima @.***> wrote:

I do not know in detail how shellcheck evaluates simplicity of a variable, but SC2086 is not issues to foo=bar; echo $foo. How about : bar; echo $_?

— Reply to this email directly, view it on GitHub https://github.com/koalaman/shellcheck/issues/2819#issuecomment-1704344369, or unsubscribe https://github.com/notifications/unsubscribe-auth/AUF2F22N6V2NEQ2CFVOJEETXYSUC7ANCNFSM6AAAAAA4A6B7QI . You are receiving this because you commented.Message ID: @.***>

wileyhy avatar Sep 03 '23 20:09 wileyhy