Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Scripts & Environment variabes

  • 10-05-2007 01:35PM
    #1
    Moderators, Science, Health & Environment Moderators Posts: 10,093 Mod ✭✭✭✭


    Just a quick question about unix shell scripts that is bugging me. I have a script used to set a few environment variables that is called by other scripts.

    All it does is set and export the variables.
    echo "In setBuildEnv.sh"
    CHECKOUT_ROOT=1.7
    export CHECKOUT_ROOT
    

    Now when I call the script like this from the other scripts or from the command line the variables are not exported.

    # ./setBuildEnv.sh
    In setBuildEnv.sh
    # echo CHECKOUT_ROOT $CHECKOUT_ROOT
    CHECKOUT_ROOT



    However when I run the command like so

    # . "setBuildEnv.sh"
    In setBuildEnv.sh
    # echo CHECKOUT_ROOT $CHECKOUT_ROOT
    CHECKOUT_ROOT 1.7

    It works just fine. I'm sure there is a simple explaination for this to do with the technical finer points of unix shell processes or something. Any unix gurus have an explaination for this?

    (Technically its a solaris box but its pretty the same thing really isn't it?)


Comments

  • Registered Users, Registered Users 2 Posts: 354 ✭✭AndrewMc


    Briefly: When you run the script directly (the first way you mention), a second shell is started and the script is run within this "subshell". This means that the environment variables are set within that subshell only, and when the script ends, the subshell exits, and your new environment disappears with it.

    When you run the script the second way, the commands are read from the file and executed within the current shell, just as if you had typed them yourself (look for the "source" command in the bash man page). Therefore, the changes to the environment are retained.

    Hopefully that makes sense!


  • Registered Users, Registered Users 2 Posts: 2,780 ✭✭✭niallb


    When you run it as
    ./setBuildEnv.sh
    you're running it in a new shell, the exported variable is exported only to that shell, and is no longer available when your prompt returns as the shell has also exited.

    When you run it as
    . setBuildEnv.sh
    you are running it within the existing shell, so the exported variable is available after exit.


  • Moderators, Science, Health & Environment Moderators Posts: 10,093 Mod ✭✭✭✭marco_polo


    Cheers lads, thought it might be something like that, its hard to google . or ./ Even though it was working just fine, it was the not knowing why that was driving me nuts. Maybe I should take a chill pill or summit :D


Advertisement