How to change the PATH
Motivation:
- You plan to make your own programs.
- You will put them in some directory.
- Normal choice is put them in $HOME/bin
- This directory must be in PATH
To put $HOME/bin in PATH, put this line:
export PATH=$PATH:$HOME/bin
in this file:
$HOME/.profile
To test it:
source $HOME/.profile
echo $PATH
Or just logout and login again.
Notes
- You could also put the line here:
$HOME/.bashrc
-
See discussion of
Linux/Unix configuration files.
- You can add more directories to PATH, separated by ":"
- You can add current directory "." to PATH.
OS programs and your programs
You type the names of OS programs and your programs.
So some issues arise:
- Can your programs use the same name as OS programs?
- If you add your dirs to PATH, should they be at start or end of PATH?
Discussion:
-
Type "which ls" to see location of the OS ls program.
-
If you write your own "ls" program,
and insert it in a dir
in the PATH which comes before the OS directory,
then
you have over-ridden the system ls.
All calls to "ls" at the command-line will be calls to your program, not system ls.
-
This may be ok for you,
but other programs that call ls
will now be calling your version instead.
They may fail in unexpected ways.
Recommended:
- If you use your own directory ($HOME/bin)
or the current directory (.)
in the PATH,
put them at end of PATH, not start.
- Do not use OS command names.
Do "which" before choosing a program name
to make sure it does not exist.
Optional experiment: "." in the PATH can be security risk
This is an advanced optional experiment to show that "." in the PATH can be a security risk.
Only advanced users should attempt this.
Do the following experiment with a friend:
- Put "." at the start of your PATH.
- Get your friend to make their home directory "r-x" for group.
- Get your friend to make a program called "ls" in their home directory.
The program is:
echo "You have been hacked."
echo "I will now rm your files."
- "cd" to your friend's home directory and type "ls"
- Now you see the problem?
- "cd" to my home directory, which is "--x" for you, and type "ls".
- That is odd too. Why?
Cleanup:
- Get your friend to set their home directory back to normal "---" for group.
- Remove "." from your PATH or put it at the end.