[TriLUG] possible awk/grep question

Paul McLanahan pmclanahan at gmail.com
Tue Jan 22 09:54:36 EST 2008


I haven't used "declare -x" to get a variable out of a script before.
Are you trying to use this script to setup an environment? I know that
you can use the $collection variable after the loop in the first
example below and it will contain the data you gave it in the loop,
but I don't know how to export that variable so that it will then be
available in your interactive shell. The only way I know to do that is
to source your script instead of run it.

#!/bin/bash
while read line
do
    # Add info to the variable
    collection="$collection $line"
done < <(command)
# do stuff with $collection ...
echo $collection

My only point was that if you tried it with the code below, that your
"$collection" variable would still be blank after the loop.

#!/bin/bash
command | while read line
do
    # Add info to the variable
    collection="$collection $line"
done
# $collection is still blank
echo $collection

Paul



More information about the TriLUG mailing list