This is almost the exact same question as in this post, except that I do not want to use eval
.
Quick question short, I want to execute the command echo aaa | grep a
by first storing it in a string variable Command='echo aaa | grep a'
, and then running it without using eval
.
In the post above, the selected answer used eval
. That works for me too. What concerns me a lot is that there are plenty of warnings about eval
below, followed by some attempts to circumvent it. However, none of them are able to solve my problem (essentially the OP's). I have commented below their attempts, but since it has been there for a long time, I suppose it is better to post the question again with the restriction of not using eval
.
Concrete Example
What I want is a shell script that runs my command when I am happy:
#!/bin/bash
# This script run-this-if.sh runs the commands when I am happy
# Warning: the following script does not work (on nose)
if [ "$1" == "I-am-happy" ]; then
"$2"
fi
$ run-if.sh I-am-happy [insert-any-command]