Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Scripts & Environment variabes

  • 10-05-2007 1:35pm
    #1
    Moderators, Science, Health & Environment Moderators Posts: 10,088 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,755 ✭✭✭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,088 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