For executing command that is stored in variable the eval command is used:
└──> a="echo -e 'a\nb' | wc -l"
└──> eval $a
2
But how can it be combined with timeout command? I've tried following which gives me wrong output:
└──> timeout 10 $a
'a
b' | wc -l
And the following which gives me errors:
└──> timeout 10 "$a"
timeout: failed to run command `echo -e \'a\\nb\' | wc -l': No such file or directory
└──> timeout 10 $(eval $a)
timeout: failed to run command `2': No such file or directory
└──> timeout 10 $(eval "$a")
timeout: failed to run command `2': No such file or directory
The question can also stand: How can I be sure that following command is executed properly?
timeout 10 "$PROGRAM" "$OPT1" "$OPT2" ...
eval "timeout 10 $a". But always remember thatevalis evil & use ofevalis always discouraged, unless there is absolutely no other way.evalwhen myPROGRAM="echo"OPT1="Hello"? I thing theevalis useful when there are pipes (as mentioned in original question) or some other awkward characters.evalevil? your argument is similar to saying:lsis evil becausefor i in $(ls)is bad. Only bad usages ofevalare bad. Now, I agree that here, OP is using an antipattern (code is not data in shell); but in the absolute,evalis no more evil than other commands. Also, there are very good usages ofeval, and I don't see why its use should be discouraged. Bad usages should be discouraged, but that's a tautology.