Find the answer to your Linux question:
Results 1 to 5 of 5
Im in a networking class and we are learning how to program some basic linux commands. But everytime we create a script we have to change the path to start ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2004
    Posts
    2

    Creating a Script to Manualy change Path


    Im in a networking class and we are learning how to program some basic linux commands. But everytime we create a script we have to change the path to start it so i was trying to create a script to change it for all the students in the class but i ran into some problems heres my code.

    echo "Enter in your directory starting after $HOME/"
    read answer
    PATH=$PATH:$HOME/$answer
    echo $PATH

    when i do this script it shows it works when i echo the $PATH but when i echo it Manualy at my promt after the scripts done it doesn't show the changes. I should also note that im telneting into a linux server to do this but i don't know if that can have an impact.

  2. #2
    Linux Newbie
    Join Date
    Aug 2001
    Location
    USA, Texas
    Posts
    217
    When you run the script it creates its own environment (simplified for clarity ??). It gets a copy of all of the current env. variables. When you modify a variable in a script it modifies only its copy and does not affect the copy being used by your shell.

    Code:
    FILE: path.scr
    
    echo $PATH
    export PATH=$PATH:$HOME/subfolder
    echo $PATH
    Code:
    bash$ echo $PATH
    /usr/bin:/usr/local/bin:/home/sykkn/bin
    bash$ sh path.scr
    /usr/bin:/usr/local/bin:/home/sykkn/bin
    /usr/bin:/usr/local/bin:/home/sykkn/bin:/home/sykkn/subfolder
    bash$ echo $PATH
    /usr/bin:/usr/local/bin:/home/sykkn/bin
    You can source a file to update your path... like what happens to the .bash_profile, your script file may work like that. Not sure but couldn't hurt to give it a try.

    sourcing: bash$ . path.scr

    //EDIT: that really is just a period and then the name of the script
    [ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ];
    /* Don\'t make me use it! */

  3. #3
    Just Joined!
    Join Date
    Apr 2004
    Posts
    2

    Thanks

    Your tip was very useful and i found another way also with both of the ideas it will work nicely.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Another option is to add code to your ~/.bash_profile - it is sourced every time you log in.

  6. #5
    Just Joined!
    Join Date
    May 2004
    Posts
    1

    Re: Thanks

    Quote Originally Posted by SlyGuy
    Your tip was very useful and i found another way also with both of the ideas it will work nicely.
    Hi!
    was going through the posts - I am facing the same problem and have not been able to solve it as yet. Could u please let me know in detail how to go about changing PATH from a script.
    A quick response will be highly appreciated.
    Thanks

Posting Permissions

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