bashpipekshsubshelldash-shell

Avoid subshell from pipe on dash


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:

How can I achieve this? Is there any simple solution?


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