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 20:59:21 +0800

Great, thanks for your explanation, but wouldn’t the literal string ‘b=$a’ be 
expanded again when eval executes it (launch a subshell?)?

> 在 2016年5月21日,下午8:38:35,Eric Blake <address@hidden> 写道:
> 
> On 05/21/2016 06:31 AM, Wu Shuangrong wrote:
>> let say:
>> $> a=‘one two     three”
> 
> Umm, that's not what you typed.  Your use of UTF-8 quotes ‘ and ” are
> NOT valid shell quotes.  Assuming you meant:
> 
> $> a='one two     three'
> 
>> $> eval ‘b=$a’
> 
> Again, that's not what you typed.  Assuming you meant:
> 
> $> eval 'b=$a'
> 
> Note the single quotes. That is the same as if you had typed:
> 
> $> b=$a
> 
> which is well-formed, and gives b the contents of $a, without word
> splitting the contents of a.
> 
>> $> printf “%s\n” “$b”
> 
> Again, not what you typed.  Assuming:
> 
> $> printf "$s\n" "$b"
> 
>> 
>> you will get “one two    three”.
> 
> Yes, that's correct, because that's what you assigned to b.
> 
>> 
>> What’s confusing me is that why this is working this way. From my point of 
>> view, after expansing,
> 
> You meant expansion, not expansing.
> 
>> 'b=$a’ will become ‘b=one two   three”,
> 
> No, that's only true if you had written:
> 
> $> eval "b=$a"
> 
> Note the difference between single and double quotes.  With single
> quotes, $ is a literal character, so you are executing the literal string:
> 
> $> b=$a
> 
> But with double quotes, you first expand the string, and the string
> includes $a, so you would be executing the literal string:
> 
> $> b=one two     three
> 
> which, at the point of the eval, would have run the command 'two' with
> argument 'three' and with 'b=one' in its environment.
> 
> -- 
> 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]