On Friday, April 12, 2013 01:14:12 PM address@hidden wrote:
Often I do something like
cmd="command arg" echo "${cmd}" >>my_log eval "${cmd}" 2>&1 >>my_log
and many times I have read on this list that storing a command in a variable
is a bad thing to do What is a better/safer way of doing this
Richard
This is perfectly correct if the value of "cmd" is correct shell code and if
you only use it together with eval. By "don't store a command in a variable",
what people usually mean is don't do this:
myCmd='someCmd -o foo --arg my\ file'
$myCmd # This is dumb and will fail.
This is smashing arguments into a string and then attempting to split
them apart using word splitting, which is completely different than eval.
Of course, there are many gotchas with eval. Aside from the usual issues, if
all else is correct, the biggest problem is that eval != lambda. You don't
bundle together an environment with the string (closure) so of course whatever
it does is entirely dependent on the context in which the string gets eval'd.