[Top][All Lists]

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

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

From: lxnf98mm
Subject: Re: [Help-bash] storing a command in a variable
Date: Fri, 12 Apr 2013 14:09:59 -0500 (CDT)
User-agent: Alpine 2.00 (LRH 1167 2008-08-23)

On Fri, 12 Apr 2013, Dan Douglas wrote:

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


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.

Yes, there have been times I could not get eval to work because the command was 
too complex an my knowledge to weak
My main objective is CYA, "Cover Your A##"
When not if something gets screwed up at least I have a chance of finding what 
caused it


reply via email to

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