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))

Have a Linux Problem
Linux Forum - Do you have a Linux Question?

Linux Books
Linux Certification, System Administration, Programming, Networking Books

Linux Home: Linux System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.