in bash, how to compute a=$(prog1) and b=$(prog2) concurrently
- User
in bash, how to compute a=(prog2) concurrently
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/out2Method 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_concurrentThe temporary file method (Method 1) is usually the most straightforward and reliable approach.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.