[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:06:00 -0500 (CDT)
User-agent: Alpine 2.00 (LRH 1167 2008-08-23)

On Fri, 12 Apr 2013, Greg Wooledge wrote:

On Fri, Apr 12, 2013 at 01:14:12PM -0500, address@hidden wrote:
Often I do something like

cmd="command arg"
echo "${cmd}" >>my_log
eval "${cmd}" 2>&1 >>my_log

Your 2>&1 is in the wrong place, if you intended for stderr to be logged
in the file.  It should appear *after* the other redirection:

eval "$cmd" >>my_log 2>&1

See for explanations.

and many times I have read on this list that storing a command in a
variable is a bad thing to do

The construction you used here is OK for simple commands with no variable
expansions in them.  It falls apart very, very quickly when you start
adding any level of complexity at all.

See for explanations.

What is a better/safer way of doing this

Since your goal is logging, the better way would be to use "set -x".
See the (second) wiki page for other suggestions for other goals.

I use "set -x" often and script


reply via email to

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