Can I use the same variable name in bash for multiple parallel background processes?

In the shell of the script below, I would like to run 2 commands in parallel in the background to speed up the process, wait for them to complete and continue to execute a few more commands.

How do I use the same variable name (DATASERVERNAME) in both loops, will these variables interfere with each other in the background? Should I use different variable names, for example DATASERVERNAME_SYBASE, DATASERVERNAME_ORACLE in each cycle?

#!/bin/bash
while read DATASERVERNAME
do
  some commands here
done < sybase_data_servers.txt &

while read DATASERVERNAME
do
  some commands here
done < oracle_data_servers.txt &

wait

some more commands here
+4
source share
1 answer

Your script is safe, as you could reveal through a little experiment:

#!/bin/sh -eu
echo A > a
echo B > b

X=0

while read X
do
  echo X=$X
done < a &
wait

echo X=$X

while read X
do
  echo X=$X
done < b &
wait



echo X=$X

Script output:

X=A
X=0
X=B
X=0

, ($BASHPID ).

+4

Source: https://habr.com/ru/post/1687400/


All Articles