Set variables in parallel in bash

后端 未结 2 1984
小蘑菇
小蘑菇 2021-01-20 20:53

Here\'s an example program:

#!/bin/bash

for x in {1..5}
do
  output[$x]=$(echo $x) &
done

wait

for x in {1..5}
do
  echo ${output[$x]}
done

2条回答
  •  清歌不尽
    2021-01-20 21:21

    If you want to avoid writing files, you can use GNU parallel:

    #!/bin/bash
    output=(`parallel -k --gnu echo {1} ::: {1..5}`)
    for i in ${output[@]}
    do
       echo $i
    done
    

    The -k is to preserve the order of the output

提交回复
热议问题