[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 06:38:35 -0600 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 |
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
signature.asc
Description: OpenPGP digital signature