Ash eval wierdness

Yann E. MORIN yann.morin.1998 at free.fr
Wed May 25 14:33:56 UTC 2011


Nigel, All,

On Wednesday 25 May 2011 162459 Nigel Hathaway wrote:
> I'm trying to import a set of variables from a file containing assignments
> of the form "a=b", one on each line. I am finding that the variable
> assignments are being lost.
> 
> Here is a simplified version of my script which illustrates the problem:
> 
> echo "myvar=my value" | while read opt; do 
>     if echo "$opt" | grep -q '='; then
>         var=`echo "$opt" | /bin/sed "s/=.*//"`
>         val=`echo "$opt" | /bin/sed "s/[^=]*=//"`
>         eval $var=\'$val\'
>         echo "(1)" $var=\'$val\'
>         echo "(2) myvar=$myvar"
>     fi
> done
> echo "(3) myvar=$myvar"
> 
> Which yields this:
> 
> (1) myvar='my value'
> (2) myvar=my value
> (3) myvar=
> 
> I am using Busybox v1.18.4
> 
> Any idea what is going on?

That's expected. All that is to the right part of the pipe is running in a
sub-shell. So the evaluation is done in a sub-shell. So it is lost as soon
as this sub-shell ends.

Regards,
Yann E. MORIN.

PS. Nigel, sorry for the duplicate, previous mail sent form the wrong
    address, so got refused by the ML.
YEM.

-- 
.-----------------.--------------------.------------------.--------------------.
|  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
| +0/33 662376056 | Software  Designer | \ / CAMPAIGN     |   ^                |
| --==< O_o >==-- '------------.-------:  X  AGAINST      |  /e\  There is no  |
| http://ymorin.is-a-geek.org/ | (*_*) | / \ HTML MAIL    |  """  conspiracy.  |
'------------------------------'-------'------------------'--------------------'


More information about the busybox mailing list