In one of my Bash scripts, there's a point where I have a variable SCRIPT
which contains the /path/to/an/exe
, and what the script ultimately needs to do, is executing that executable. Therefore the last line of the script is
$($SCRIPT)
so that $SCRIPT
is expanded to /path/to/an/exe
, and $(/path/to/an/exe)
executes the executable.
However, running shellcheck
on the script generates this error:
In setscreens.sh line 7:
$($SCRIPT)
^--------^ SC2091: Remove surrounding $() to avoid executing output.
For more information:
https://www.shellcheck.net/wiki/SC2091 -- Remove surrounding $() to avoid e...
Is there a way I can rewrite that $($SCRIPT)
in a more appropriate way? eval
does not seem to be of much help here.
$($SCRIPT)
indeed does not do what you think it does.The outer
$()
will execute any commands inside the parenthesis and execute the result string.The inner
$SCRIPT
will expand to the value of theSCRIPT
variable and execute this string while splitting words on spaces/If you want to execute the command contained into the
SCRIPT
variable, you just write as an example:Now if you also need to handle arguments with your
SCRIPT
variable command call:To also store or build arguments dynamically, you'd need an array instead of a string variable.
Example: