Find the answer to your Linux question:
Results 1 to 5 of 5
The following script is expected to update the $PATH environment variable with the result of 'pwd' command, however it does not work on CentOS 5.2: #/bin/bash export working_path=`pwd` #echo $working_path ...
  1. #1
    Just Joined!
    Join Date
    Nov 2007
    Posts
    8

    scripting help

    The following script is expected to update the $PATH environment variable with the result of 'pwd' command, however it does not work on CentOS 5.2:

    #/bin/bash
    export working_path=`pwd`
    #echo $working_path
    export PATH=$PATH:$working_path



  2. #2
    Linux Newbie danielsmw's Avatar
    Join Date
    Nov 2006
    Location
    Clemson, SC / Charleston, SC
    Posts
    110
    Are you sure? Try echo'ing $PATH at the end of that script and see what it says. Note that (to the best of my knowledge, I could be wrong) a script like this will run in its own sub-shell of your original shell. Therefore, when this script exists, there will appear to be no change to the environment variables of your original, interactive shell.

    If you want to change your "normal" shell, try just executing
    Code:
    export PATH=$PATH:`pwd`
    at the command prompt. That should achieve the correct effect. (You can also do it this way in your script, eliminating the need for the working_path variable.)
    Registered Linux User: #479567
    Asking a question? Read this page first.
    Now... sudo make me a sandwich.
    ratiocinativeroot.blogspot.com

  3. #3
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    As danielsmw mentioned when you normally execute a script it runs in a child process so the change isn't in your shell environment. To execute the script in the current shell environment use either method:

    Code:
    source /path/to/script
    or

    Code:
    . /path/to/script

  4. #4
    Just Joined!
    Join Date
    Nov 2007
    Posts
    8
    You're right the shell created by the script might not be allowing the $PATH to be updated within the "normal" shell.

    Normal shell > calls > Shell generated by script.

    The command suggested actually updates the PATH variable. Thanks for your help.

    Quote Originally Posted by danielsmw View Post
    Are you sure? Try echo'ing $PATH at the end of that script and see what it says. Note that (to the best of my knowledge, I could be wrong) a script like this will run in its own sub-shell of your original shell. Therefore, when this script exists, there will appear to be no change to the environment variables of your original, interactive shell.

    If you want to change your "normal" shell, try just executing
    Code:
    export PATH=$PATH:`pwd`
    at the command prompt. That should achieve the correct effect. (You can also do it this way in your script, eliminating the need for the working_path variable.)

  5. #5
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    If you want to do this, use a function. You can define something like this on your .bashrc/.bash_profile,

    Code:
    # Function to add a path
    # Usage: add_path <path>
    add_path () {
      if [ "$PATH" == "${PATH/$1/}" ]
      then
        PATH="$1:$PATH"
        export PATH
      fi
    }
    Functions will run on the same shell you invoke them. So, you can just do:

    Code:
    add_path "/home/foo/my custom path"

Posting Permissions

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