help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] storing a command in a variable


From: Dan Douglas
Subject: Re: [Help-bash] storing a command in a variable
Date: Fri, 12 Apr 2013 13:36:08 -0500
User-agent: KMail/4.8.3 (Linux/3.4.6-pf+; KDE/4.8.3; x86_64; ; )

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.
-- 
Dan Douglas



reply via email to

[Prev in Thread] Current Thread [Next in Thread]