I’d like to know why this works:
arr=()
fun() { arr[$1]=$2; }
fun 1 2
echo ${arr[1]}
# echoes '2'
but this doesn’t:
arr=()
fun() { arr[$1]=$2; }
fun 1 2 &
wait
echo ${arr[1]}
# echoes a blank line
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
By running
funin the background in your second example, you run it in a subshell. Changes to the array made in the subshell are not visible to the parent shell, where you echo the value ofarr[1].