Document your code
Every project on GitHub comes with a version-controlled wiki to give your documentation the high level of care it deserves. It’s easy to create well-maintained, Markdown or rich text documentation alongside your code.
Sign up for free See pricing for teams and enterprisesSC2091
Remove surrounding $() to avoid executing output.
Problematic code:
if $(which epstopdf)
then
echo "Found epstopdf"
fiCorrect code:
if which epstopdf
then
echo "Found epstopdf"
fiRationale:
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.
The solution is simply to remove the surrounding $(). This will execute the command instead of the command's output.
Exceptions:
If you really want to execute the output of a command rather than the command itself, you can ignore this message or assign the output to a new variable first:
readonly command_to_execute="$(print_the_command)"
$command_to_executeRelated resources:
- StackOverflow: Bash Function -> Command not found