$()
to avoid executing output (or use
eval
if intentional).if $(which epstopdf)
then
echo "Found epstopdf"
fi
or
make_command() {
printf 'cat header %q footer > %q\n' "$1" "$2" | tee log
}
$(make_command foo.html output/foo.html)
if which epstopdf
then
echo "Found epstopdf"
fi
or
make_command() {
printf 'cat header %q footer > %q\n' "$1" "$2" | tee log
}
eval "$(make_command foo.html output/foo.html)"
ShellCheck has detected that you have a command that just consists of
a command substitution. This often happens when you want to run a
command (possibly from a variable name), without realizing that
$(..)
is for capturing and not for executing.
For example, if you have this shell function:
sayhello() { echo "hello world"; }
Then $(sayhello)
will:
sayhello
, capturing "hello world"hello world
, resulting in
bash: hello: command not found
Meanwhile, just sayhello
will:
sayhello
, outputting "hello world" to screenNote that this is equally true if the command is in a variable, e.g.
x=sayhello; $($x)
.
If you do have a command that outputs a second command,
similar to how ssh-agent
outputs export
commands to run, then you should do this via eval
. This
way, quotes, pipes, redirections, semicolons, and other shell constructs
will work as expected. Note that this kind of design is best avoided
when possible, since correctly escaping all values can be difficult and
error prone.
None.
ShellCheck is a static analysis tool for shell scripts. This page is part of its documentation.