Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-19-2009, 05:29 AM   #1
LQ Newbie
Registered: Nov 2009
Distribution: Ubuntu (latest)
Posts: 2

Rep: Reputation: 0
shell script returning counter value from a while loop - lost upon completion

Hi all

I need to know how to return a counter variable from a while loop.
Variable value is lost when exiting the while loop.

This is a stupid exacmple just to demonstrate the problem.

### ---------------------------- begin
typeset -x -i v=0

cat .bashrc | grep "^#alias" | while read line
v=`expr $v + 1`
echo "found $v $line"

echo "Total occurrences: $v"
### ---------------------------- end

When you run it, you'll get:

$ .
found 1 #alias ll='ls -l'
found 2 #alias la='ls -A'
found 3 #alias l='ls -CF'
Total occurrences: 0

Any help would be appreciated.
Old 11-19-2009, 05:41 AM   #2
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
A classic bash "gotcha"!

When bash runs a pipeline it runs all but the first command in subshell(s) and any variables set in them are lost when the pipeline completes. Explained in more detail with workarounds here.
Old 11-19-2009, 06:30 AM   #3
LQ Newbie
Registered: Nov 2009
Distribution: Ubuntu (latest)
Posts: 2

Original Poster
Rep: Reputation: 0
Smile Thx catkin

This indeed does the trick.

cat /etc/passwd |
while read -r line ; do
echo "total number of lines: $linecnt"


counter, loop, return, script, shell, value, while

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script , while loop ykc Programming 5 03-30-2009 07:50 AM
Shell Script skipping a loop dnvikram Programming 2 01-23-2009 02:29 PM
Loop in Shell Script delamatrix Programming 4 07-24-2008 05:20 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
Shell Script: A Broadcast Packet Counter BigglesZX Programming 12 06-15-2006 02:23 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:43 AM.

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