Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SC2068 should warn on unquoted :+ parameter expansions #1835

Open
2 tasks done
dimo414 opened this issue Feb 13, 2020 · 1 comment
Open
2 tasks done

SC2068 should warn on unquoted :+ parameter expansions #1835

dimo414 opened this issue Feb 13, 2020 · 1 comment

Comments

@dimo414
Copy link
Contributor

dimo414 commented Feb 13, 2020

For bugs

  • Rule Id (if any, e.g. SC1000): SC2068
  • My shellcheck version (shellcheck --version or "online"):
  • The rule's wiki page does not already cover this (e.g. https://shellcheck.net/wiki/SC2086)
  • I tried on 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

bar=
foo --bang=$bar
foo ${bar:+--bang=$bar}
foo ${bar:+"--bang=$bar"}
foo "${bar:+--bang=$bar}"

Here's what shellcheck currently says:

Line 4:
foo --bang=$bar
           ^-- SC2086: Double quote to prevent globbing and word splitting.

Here's what I wanted or expected to see:

Line 5 should similarly trigger, as it is also subject to word splitting. By contrast line 6 (correctly) does not trigger because the inner expansion is properly quoted. This has the effect of only passing an argument to foo if $bar is set, and otherwise no arguments are passed.

As @eatnumber1 notes below, line 7 is also safe because the entire expansion is quoted, but will result in an empty-string argument being passed to foo, which isn't necessarily desirable.

@eatnumber1
Copy link
Contributor

Worth noting that ShellCheck does the right thing (no warnings) if the entire expansion is quoted. E.g. the following produce no warnings and each printf correctly prints foo bar with no line breaks.

foo="baz foobaz"
printf '%s\n' "${foo:+foo bar}"
printf '%s\n' "${foo:+"foo bar"}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants