Help answer threads with 0 replies.
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 08-26-2010, 09:14 AM   #1
LQ Newbie
Registered: Aug 2010
Posts: 2

Rep: Reputation: 0
Question [BASH] non-empty variable before loop end, is empty after exiting loop

Hi fellows:

I'm having a quite disturbing trouble with this simple script:



find $DIR_HTML_UTF -type f -name "*html" | sed 's/[^0-9]//g' | sort -u | while read vid; do
	echo $vid
	echo "  >> ${vids:1}"

echo "=== $vids ==="
And the result of the script is:

  >> 101993
  >> 101993,150115
  >> 101993,150115,150188
  >> 101993,150115,150188,32416
  >> 101993,150115,150188,32416,93939
===  ===
I can't understand why $vids is empty after de loop. It's just too weird, isn't it?

I'm using a Ubuntu 10.04 64bits box.

Old 08-26-2010, 09:30 AM   #2
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
It's a simple and common mistake. You used a pipe to feed the output of find into the while loop, which means it runs in a subshell. The subshell can't affect the parent process, so the settings are lost when it exits.

You need to reposition the input as a redirect instead. Something like this (untested):
while read vid; do
	echo $vid
	echo "  >> ${vids:1}"
done <<<"$(find $DIR_HTML_UTF -type f -name "*html" | sed 's/[^0-9]//g' | sort -u )"
It's #8 on this list, which includes a link to a more detailed explanation.
1 members found this post helpful.
Old 08-26-2010, 09:57 AM   #3
LQ Newbie
Registered: Aug 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks a lot, now i see. I was expecting something like that, so i tried to "export" the variable in some way.

I searched for the answer at LQ, but was sort of difficult to figure out how to search such a problem (loop + variable + empty ?). Sorry for reiteration.


bash, empty, loop, pipe, subshell, variable

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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
Multiple variable for loop (bash) Phier Programming 9 11-24-2009 08:54 AM
Multiple variable for loop (bash) Phier Linux - Newbie 2 11-23-2009 11:13 AM
Using a variable to control a BASH for loop JohnE1 Programming 15 07-04-2009 03:37 AM
Deleting empty line at end of text file in BASH human2.0 Linux - General 8 04-01-2009 02:44 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:29 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
Open Source Consulting | Domain Registration