mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Updated SC2091 (markdown)
22
SC2091.md
22
SC2091.md
@@ -2,16 +2,30 @@
|
||||
|
||||
### Problematic code:
|
||||
|
||||
if $(which "false"); then echo "true"; fi
|
||||
if $(which epstopdf)
|
||||
then
|
||||
echo "Found epstopdf"
|
||||
fi
|
||||
|
||||
### Correct code:
|
||||
|
||||
if which "false" >/dev/null >2&1; then echo "true"; fi
|
||||
if which epstopdf
|
||||
then
|
||||
echo "Found epstopdf"
|
||||
fi
|
||||
|
||||
### Rationale:
|
||||
|
||||
If the `$()` subshell produces an output it will be evaluated by the `if` statement, this could result in the execution of the output, which will result in different behaviour than intended.
|
||||
ShellCheck has detected that you have a command that just consists of a command substitution.
|
||||
|
||||
This is typically done in order to try to get the shell to execute a command, because `$(..)` does indeed execute commands. However, it's also replaced by the output of that command.
|
||||
|
||||
When you run `echo "The date is $(date +%F)"`, bash evalutes the `$(..)`. The command then becomes `echo "The date is 2015-04-29"`, which writes out the string `The date is 2015-04-29`
|
||||
|
||||
The problem is when you use `$(date +%F)` alone as a command. Bash evaluates the `$(..)`, and the command then becomes `2015-04-29`. There is no command called `2015-04-29`, so you get `bash: 2015-04-29: command not found`.
|
||||
|
||||
Sometimes this results in this confounding `command not found` messages. Other times you get even stranger issues, like the example problematic code which always evaluates to false.
|
||||
|
||||
### Exceptions:
|
||||
|
||||
None.
|
||||
If you really want to execute the output of a command rather than the command itself, you can ignore this message.
|
Reference in New Issue
Block a user