Linux Certification, System Administration, Programming, Networking Books
Return Variables from a Piped Function

Would like to return variables from a piped function to the script that calls it.

Example
------------------------
cat $file_name | \
while read line
do
all_lines="$all_lines $line"
done
echo $all_lines
------------------------

This is outputing nothing because all_lines variable in the piped while is not the same as the one after.

Now i tried this
------------------------
declare -x all_lines
cat $file_name | \
while read line
do
export all_lines="$all_lines $line"
done
echo $all_lines
------------------------

Not working either.

Does someone know a simple way to return variables to the script?!?!?
 

Don't call external programs to read files unless they are acting as filters:
while read line; do all="$all $line"; done < $file_name; echo $all

...is the way to use read in a while loop although to do what your script does it isn't even necessary:

all=$(<$file_name); echo $all
 

...and if it was a filter, called from inside a script, just read it in a subshell:
filter $file_name | (while read....)
 

ie:
output=$(filter $file_name | (while read line; do all="$all $line"; done; echo $all))

Quick Links:
Do you have a Linux Question?

Linux Home: Linux System Administration Hints and Tips