Today I got stuck trying to use pipe inside a command variable in a bash script:
$CMD="echo foo | cat" EXEC=`$CMD` # expecting foo echo $EXEC # got foo | cat
I could solve that if I run the command directly, without variables:
EXEC=`echo foo | cat` # expecting foo echo $EXEC # got foo
But that was not an option, because
$CMD
was complex and carefully mounted before that snippet.Googling around I found that the solution is to use
eval
(although I used it slightly different from the source):$CMD="echo foo | cat" EXEC=`eval $CMD` # expecting foo echo $EXEC # got foo
The reason is that
eval
can expand the pipe command inside a variable, while backticks can't.Cheers!
No comments:
Post a Comment