shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

feature request: detect that the single quotes in "${var:='.'}" are literal

Open emanuele6 opened this issue 3 years ago • 2 comments

For new checks and feature suggestions

  • [x] https://www.shellcheck.net/ (i.e. the latest commit) currently gives no useful warnings about this
  • [x] 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/sh --

: "${var='.'}"

Here's what shellcheck currently says:

nothing

Here's what I wanted or expected to see:

: "${var='.'}"
         ^-- SCXXXX (warning): single quotes inside double quoted parameter expansions are literal, remove the single quotes and quote special characters using `\` instead.

I have seen many different people making this mistake recently.

It can be counterintuitive, but single quotes in the word of a ${par=word}, ${par-word}, or ${par+word} parameter expansion in double quotes are actually literal.

It would be nice if shellcheck was able to spot this mistake (since I have also had problems with this in the past).

: "${var="'.'"}" can be suggested as a way to mute the error if the literal single quotes are intended, since the redundant double quotes would be removed.

Note that this also applies to:

#!/bin/sh --
set --
# the ' characters are literal.
var="${1-'.'}"
printf '$var is %s\n' "$var" # $var is '.'

but not to:

#!/bin/sh --
set --
# the ' characters are removed (but redundant since '.' is not special)
var=${1-'.'}
printf '$var is %s\n' "$var" # $var is .

emanuele6 avatar Aug 16 '22 10:08 emanuele6

I also noticed that "${var1:-'$var2'}" triggers a false positive for SC2016.

emanuele6 avatar Aug 18 '22 05:08 emanuele6

I ran into this myself, as I didn't expect single quotes to be taken literally, and a bug resulted from it that required some fair amount of recovery (the resulting string was used in a CI/CD context). So, this is definitely something that would be useful for Shellcheck to alert on.

erikeckhardt avatar Sep 01 '22 19:09 erikeckhardt