I have this example code:
find "$1" ! -regex "$regex" 2>/dev/null | while read line ; do
a="$line"
done
echo ("$a") # prints nothing because of subshell
I need:
$a
visible outside (in global scope)How can I achieve this? Is there any simple solution?
Use an explicit named pipe.
mkfifo named_pipe
find "$1" ! -regex "$regex" 2> /dev/null > named_pipe &
while read line; do
a=$line
done < named_pipe