help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Is there a way to delay variable substitution using eval


From: Greg Wooledge
Subject: Re: [Help-bash] Is there a way to delay variable substitution using eval?
Date: Wed, 28 Jan 2015 11:03:04 -0500
User-agent: Mutt/1.4.2.3i

On Wed, Jan 28, 2015 at 09:49:55AM -0600, Peng Yu wrote:
> I want to delay the variable substitution. Is there a way to do so in
> bash? Thanks.

I'm not sure I understand your question.

> ~$ x=xyz
> ~$ eval "echo $x" # variable will be substituted before eval is
> called, which is not what I need
> ~$ eval "echo $$x" # This does not work.
> 6592x

The value of x does not change, and the value doesn't contain any shell
metacharacters.  I don't see why it matters whether x is evaluated before
or after eval.

Also, your examples are not very good.  Don't use misleading examples.
Use your actual code.  You haven't even shown WHY you're using eval!
The best rewrite of your example would be simply:  echo "$x"

That said, if you actually DO need eval, and if there is some REASON you
would care whether x is evaluated before or after eval, you can either
change your quotes or escape the $ sign.

eval 'echo "$x"'      # preferred
eval "echo \"\$x\""   # pain in the ass

I'm guessing this is just the tip of the iceberg, though.  If you're
asking for a general tutorial on how to deal with the pain and agony of
eval, we're going to have to type a LOT more.



reply via email to

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