This can be done with coprocesses in bash 4.x, if your script1
and script2
write their variables to stdout:
#!/usr/bin/env bash
# ^^^- Must be bash, not /bin/sh; must NOT start this script with "sh name".
start_seconds=$SECONDS
run_script1() { sleep 5; echo "one"; } # replace these with your script1 or script2
run_script2() { sleep 5; echo "two"; }
coproc coproc_script1 { run_script1; }
coproc coproc_script2 { run_script2; }
read -u "${coproc_script1[0]}" var1
read -u "${coproc_script2[0]}" var2
echo "End time: $(( SECONDS - start_seconds )); var1=$var1; var2=$var2"
If the values weren't running in parallel, your End time
would be 10 or more. Instead, it should in practice be 5
or 6
with the above code.