Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-sonnet-4-20250514· AnthropicHugging Face LMArena Human Preference 140K

in bash, how to compute a=$(prog1) and b=$(prog2) concurrently

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with MiniMaxminimax-m1’s answer.
  1. User

    in bash, how to compute a=(prog1)andb=(prog1) and b=(prog2) concurrently

  2. Claude Assistant

    You can run the commands concurrently using background processes and then wait for them to complete:

    prog1 & pid1=$!
    prog2 & pid2=$!
    wait $pid1; a=$(prog1)
    wait $pid2; b=$(prog2)
    

    However, this approach runs the programs twice. A better method is to capture output from background processes using temporary files or process substitution:

    Method 1: Using temporary files

    prog1 > /tmp/out1 & pid1=$!
    prog2 > /tmp/out2 & pid2=$!
    wait $pid1 $pid2
    a=$(cat /tmp/out1)
    b=$(cat /tmp/out2)
    rm /tmp/out1 /tmp/out2
    

    Method 2: Using process substitution with exec

    exec 3< <(prog1)
    exec 4< <(prog2)
    a=$(cat <&3)
    b=$(cat <&4)
    exec 3<&- 4<&-
    

    Method 3: Using a function wrapper

    run_concurrent() {
        local tmp1=$(mktemp)
        local tmp2=$(mktemp)
        prog1 > "$tmp1" & local pid1=$!
        prog2 > "$tmp2" & local pid2=$!
        wait $pid1 $pid2
        a=$(cat "$tmp1")
        b=$(cat "$tmp2")
        rm "$tmp1" "$tmp2"
    }
    run_concurrent
    

    The temporary file method (Method 1) is usually the most straightforward and reliable approach.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.