Results 1 to 3 of 3
Hi
I'm trying to write a shell script using while read loop. The part that bothers me is this:
Code:
#!/bin/sh
n=0
echo abc | while read line ; do
...
- 04-20-2010 #1
Bash - global variable?
Hi
I'm trying to write a shell script using while read loop. The part that bothers me is this:
When executed, it gives output as follows:Code:#!/bin/sh n=0 echo abc | while read line ; do n=$[n + 1] echo $n done echo $n
- the value of n outside the loop doesn't change. What should I do to make that variable global?Code:1 0
Thanks,
phosphide
- 04-21-2010 #2Linux Newbie
- Join Date
- Sep 2004
- Location
- UK
- Posts
- 160
replace `ls` with static text or whatever you needCode:#!/bin/sh n=0 while read line do n=$((n + 1)) echo "In loop $n" done << MY_EOF `ls` MY_EOF echo "at end $n"In a world without walls and fences, who needs Windows and Gates?
- 04-21-2010 #3


Reply With Quote