LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-12-2012, 03:09 PM   #1
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
BASH: variables inside "while" loop are local ?


Hi,

This is fine.No news here.
Code:
a=2; for i in $(seq 1 3) ; do echo a=$a; a=1; done; echo a=$a
a=2
a=1
a=1
a=1
But this is unexpected - at least for me!
Code:
a=2; seq 1 3 | while read f; do echo a=$a; a=1; done; echo a=$a
a=2
a=1
a=1
a=2
Looks like "a" is a global variable inside the while loop before the assignment, and a local variable after the assignment. At end, "a" still has the original value.

How can I use a global variable inside the "while" loop ? I want a behavior like the first one that use a "for" loop.
 
Old 04-12-2012, 07:17 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Whe you use a pipe to join commands, each command is run in a subshell.
 
Old 04-13-2012, 03:25 PM   #3
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499

Original Poster
Blog Entries: 2

Rep: Reputation: 68
Hi kbp,

uhmm...so exporting variable "a" should work, but not.
Code:
export a; a=2; seq 1 3 | while read f; do echo a=$a; a=1; done; echo a=$a
a=2
a=1
a=1
a=2
Do you have a explanation ? Or better yet, a solution to this problem ? I mean, at end of "while" loop, how can I have access to variables that was set inside the loop ?
 
  


Reply

Tags
bash, global



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH script with sed file "inside"? SilversleevesX Linux - Newbie 6 02-25-2010 10:31 PM
Elements in a "for loop" using bash not being separated by white spaces. ralste Programming 6 11-10-2009 02:26 AM
bash: variables inside while loop J_Szucs Programming 5 07-19-2009 08:24 AM
[SOLVED] How to get "case" to overwrite preset variables in a bash script Mogget Programming 4 02-24-2009 01:19 PM
bash script: how do I refer to a counter i in a "for i in" loop frankie_DJ Programming 5 07-24-2007 08:45 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration