help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] eval


From: Wu Shuangrong
Subject: Re: [Help-bash] eval
Date: Sat, 21 May 2016 21:28:39 +0800

Thank you very much, it’s very clear.

> 在 2016年5月21日,下午9:19:17,Eric Blake <address@hidden> 写道:
> 
> On 05/21/2016 06:59 AM, Wu Shuangrong wrote:
>> 
>> Great, thanks for your explanation, but wouldn’t the literal string ‘b=$a’ 
>> be expanded again when eval executes it (launch a subshell?)?
> 
> [please don't top-post on technical lists]
> 
> 'eval' does NOT execute subshells.  It evaluates text in the current
> shell environment.  What text it evaluates matches exactly what echo
> would show.  So if you want to know what eval will run, first try
> echoing the string, then pretend you had typed that string.
> 
> $> eval 'b=$a'
> 
> will execute the results of
> 
> $> echo 'b=$a'
> b=$a
> 
> which is a simple variable assignment.  You could type
> 
> $> b=$a
> 
> and get the same result, without eval in the mix.
> 
> $> eval "b=$a"
> 
> will execute the results of
> 
> $> echo "b=$a"
> b=one two     three
> 
> which is the same as if you had typed
> 
> $> b=one two three
> 
> without eval in the mix.
> 
> Remember, just as your choice of '' vs. "" quoting matters to echo, it
> also matters to eval.  Also remember that "" is optional during variable
> assignment, because that is a context where word splitting is not
> performed. That is, the following two commands are identical:
> 
> $> b=$a
> $> b="$a"
> 
> -- 
> Eric Blake   eblake redhat com    +1-919-301-3266
> Libvirt virtualization library http://libvirt.org
> 





reply via email to

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