Search code examples
bashio-redirectionsubshell

Bash subshell input with variable number of subshells


I want to grep lines from a variable number of log files and connect their outputs with paste. If I had a fixed number of outputs, I could do it thus:

paste <(grep $PATTERN $FILE1) <(grep $PATTERN $FILE2)

But is there a way to do this with a variable number of input files? I want to write a shell script whose arguments are the input files. The shell script should paste the grepped lines from ALL of them.


Solution

  • Use explicit named pipes, instead of process substitution.

    pipes=()
    for f in "$FILE1" "$FILE2" "$FILE3"; do
        n="$(mktemp)"  # Or some other command to create a temporary name
        mkfifo "$n"
        pipes+=( "$n" )
        grep "$PATTERN" "$f" > "$n" &
    done
    
    paste "${pipes[@]}"
    rm  "${pipes[@]}"   # When done with them