help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] eval


From: Eric Blake
Subject: Re: [Help-bash] eval
Date: Sat, 21 May 2016 07:19:17 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0

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

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

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