PDA

View Full Version : parent/child inheritance in bash



mo.reina
November 26th, 2009, 12:35 PM
can someone clarify this for me.

AFAIK, any variables in child processes are not passed to the parent. this includes the value ofvariables in any do-done constructs (while, for, until loops).

i've seen this first hand when i started scripting, the values of variables in loops weren't being passed on to the main body of the script.

however, i was writing something today and absentmindedly did this

while true; do

number=$RANDOM
if (( $number > 100 )) || (( $number == 0 )); then
continue
else
break
fi
done

echo $number

and the script actually gave me the value of $number. what gives?

Arndt
November 26th, 2009, 02:32 PM
can someone clarify this for me.

AFAIK, any variables in child processes are not passed to the parent. this includes the value ofvariables in any do-done constructs (while, for, until loops).

i've seen this first hand when i started scripting, the values of variables in loops weren't being passed on to the main body of the script.

however, i was writing something today and absentmindedly did this

while true; do

number=$RANDOM
if (( $number > 100 )) || (( $number == 0 )); then
continue
else
break
fi
done

echo $number

and the script actually gave me the value of $number. what gives?

This seems normal. That child processes don't inherit variables is normal too - if you use 'export', they do, however.

What you write about "values of variables in loops weren't being passed on to the main body of the script" seems strange, on the other hand.

mo.reina
November 26th, 2009, 03:22 PM
i think i confused the do-done loops with pipelines, thinking they followed the same rules.

i always close file descriptors with every pipeline and/or command, to avoid inheritance issues. since the pipelines/commands open sub-shells, i thought that the loops followed the same rule. don't know how that came about....