shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

Incorrect suggestion for `` `(foo; bar)` ``

Open lemzwerg opened this issue 3 years ago • 3 comments

For bugs

  • Rule Id (if any, e.g. SC1000): SC2148, SC2006, SC2164
  • My shellcheck version (shellcheck --version or 'online'): online
  • [x] I tried on shellcheck.net and verified that this is still a problem on the latest commit
  • [ ] It's not reproducible on shellcheck.net, but I think that's because it's an OS, configuration or encoding issue

For new checks and feature suggestions

  • [x] 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:

srcdir=`(cd "$srcdir"; pwd)`

Here's what shellcheck currently says:

Line 1	SC2148: Tips depend on target shell and yours is unknown. Add a shebang or a 'shell' directive.
Line 1	SC2006: Use $(...) notation instead of legacy backticks `...`.
Line 1	SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

Here's what I wanted or expected to see:

The suggestion output is

Did you mean: [...]
srcdir=$((cd "$srcdir" || exit; pwd))

which is wrong AFAIK; $((...)) is for arithmetics.

lemzwerg avatar Jul 24 '22 18:07 lemzwerg

You're right, this is due to ShellCheck's naive replacement of `..` with $(..) without considering that the .. might be a redundant explicit subshell (..) causing the expression to become $((..)).

Is there a particular reason why you're using (..) here? It's entirely redundant since backticks already create a subshell. This might be a useful check in itself.

koalaman avatar Jul 24 '22 23:07 koalaman

No reason – the code just evolved :-) And yes, this might be a useful check.

lemzwerg avatar Jul 25 '22 03:07 lemzwerg

BTW, there might be constructions like

`(foo; bar) 2>&1`

which means that parentheses might be useful within backticks...

lemzwerg avatar Jul 25 '22 04:07 lemzwerg