Results 1 to 3 of 3

Thread: parent/child inheritance in bash

  1. #1

    parent/child inheritance in bash

    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
    Code:
    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?

  2. #2
    Join Date
    May 2006
    Beans
    1,787

    Re: parent/child inheritance in bash

    Quote Originally Posted by mo.reina View Post
    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
    Code:
    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.

  3. #3

    Re: parent/child inheritance in bash

    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....

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •