Adding a Directory to the Path

Embed Size (px)

Citation preview

  • 8/11/2019 Adding a Directory to the Path

    1/5

    Troubleshooters.Com and T.C Linux Library Present

    Adding a Directory to the PathCopyright (C) 2002 by Steve Litt

    [ Linux Library | Troubleshooters.Com | Email Steve Litt | Copyright Notice ]

    Debug like a NinjaContents:

    Disclaimer Pre and Post Pathing

    Adding to a Single User's Path

    Adding to All Users' Paths (except root) Adding to the Path of User rootSummary

    Executive Summary Adding a directory to the path of a user or all users wouldseem trivial, but in fact it isn't. The best place to add adirectory to the path of a single user is to modify that user's.bash_profile le. To add it to all users except user root , add it to/etc/profile . To also add it to the path of user root , add it to root 's.bash_profile le.

    Disclaimer

    ng a Directory to the Path http://www.troubleshooters.com/linux/prepostpath.htm

    08/02/2013 10:27 A

  • 8/11/2019 Adding a Directory to the Path

    2/5

    Obviously, you use this document at your own risk. I am notresponsible for any damage or injury caused by your use of thisdocument, or caused by errors and/or omissions in this document. If that's not acceptable to you, you may not use this document. Byusing this document you are accepting this disclaimer.

    Pre and Post PathingLinux determines the executable search path with the $PATHenvironment variable. To add directory /data/myscripts to the beginningof the $PATH environment variable, use the following:

    PATH=/data/myscripts:$PATH

    To add that directory to the end of the path, use the followingcommand:PATH=$PATH:/data/myscripts

    But the preceding are not sufficient because when you set anenvironment variable inside a script, that change is effective onlywithin the script. There are only two ways around this limitation:

    If, within the script, you export the environment variable it is

    effective within any programs called by the script. Note that it isnot effective within the program that called the script.

    1.

    If the program that calls the script does so by inclusion insteadof calling, any environment changes in the script are effectivewithin the calling program. Such inclusion can be done with thedot command or the source command. Examples:. $HOME/myscript.sh

    source $HOME/myscript.sh

    2.

    Inclusion basically incorporates the "called" script in the "calling"script. It's like a #include in C. So it's effective inside the "calling"script or program. But of course, it's not effective in any programs orscripts called by the calling program. To make it effective all the waydown the call chain, you must follow the setting of the environment

    variable with an export command.

    As an example, the bash shell program incorporates the contents of

    ng a Directory to the Path http://www.troubleshooters.com/linux/prepostpath.htm

    08/02/2013 10:27 A

  • 8/11/2019 Adding a Directory to the Path

    3/5

    le .bash_profile by inclusion. So putting the following 2 lines in.bash_profile :PATH=$PATH:/data/myscriptsexport PATH

    effectively puts those 2 lines of code in the bash program. So withinbash the $PATH variable includes $HOME/myscript.sh , and because of the exportstatement, any programs called by bash have the altered $PATH variable.

    And because any programs you run from a bash prompt are called bybash , the new path is in force for anything you run from the bashprompt.

    The bottom line is that to add a new directory to the path, you mustappend or prepend the directory to the $PATH environment variablewithin a script included in the shell, and you must export the $PATH

    environnment variable. The only remaining question is: In whichscript do you place those two lines of code?

    Adding to a Single User's PathTo add a directory to the path of a single user, place the lines in thatuser's .bash_profile le. Typically, .bash_profile already contains changes tothe $PATH variable and also contains an export statement, so you cansimply add the desired directory to the end or beginning of theexisting statement that changes the $PATH variable. However, if .bash_profile doesn't contain the path changing code, simply add thefollowing two lines to the end of the .bash_profile le:

    PATH=$PATH:/data/myscriptsexport PATH

    Adding to All Users' Paths(except root) You globally set a path in /etc/profile . That setting is global for all usersexcept user root . Typical /etc/profile les extensively modify the $PATH

    variable, and then export that variable. What that means is you canmodify the path by appending or prepending the desired directory(s)

    ng a Directory to the Path http://www.troubleshooters.com/linux/prepostpath.htm

    08/02/2013 10:27 A

  • 8/11/2019 Adding a Directory to the Path

    4/5

  • 8/11/2019 Adding a Directory to the Path

    5/5

    Copyright (C) 2002 by Steve Litt. -- Legal

    ng a Directory to the Path http://www.troubleshooters.com/linux/prepostpath.htm