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: Greg Wooledge
Subject: Re: [Help-bash] storing a command in a variable
Date: Fri, 12 Apr 2013 14:34:09 -0400
User-agent: Mutt/1.4.2.3i

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 http://mywiki.wooledge.org/BashFAQ/055 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 http://mywiki.wooledge.org/BashFAQ/050 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.



reply via email to

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