On Friday 12 September 2008, Adam wrote:
> I pared down a bash script to the minimum that still has a problem:
>
> [EMAIL PROTECTED] ~]$ cat d
> #!/bin/bash
> CONTENTS="xyz"
> N=1
> echo "$CONTENTS" | while read LINE ; do
>         N=4
>         echo N = $N
> done
> echo N is now $N
>
> [EMAIL PROTECTED] ~]$ ./d
> N = 4
> N is now 1
> [EMAIL PROTECTED] ~]$
>
> How come N returns to its original value when the 'while' loop ends?
> How can I get it to keep its new value?  Adding "export N" inside the
> loop made no difference.  Thanks, anybody!

This is a problem I've run into relatively often, and there's a workaround 
that you can use by capturing output of the prior shell into a variable in 
the current shell:


#!/bin/bash
CONTENTS="xyz"
N=1
N=$(echo "$CONTENTS" | while read LINE ; do
        N=4
        echo $N
done)
echo N is now $N



Running the above results in:
N is now 4


  -- Chris

-- 

Chris Knadle
[EMAIL PROTECTED]
_______________________________________________
Mid-Hudson Valley Linux Users Group                  http://mhvlug.org          
   
http://mhvlug.org/cgi-bin/mailman/listinfo/mhvlug                           
Upcoming Meetings (6pm - 8pm)                         MHVLS Auditorium          
        
  Sep 3 - Porkchop - The Areas of My Expertise
  Oct 1 - Ubikeys
  Oct 4 - Linux Fest
  Nov 5 - Releasing Open Source Software
  Dec 3 - TBD
  

Reply via email to