Reputation: 1348
I have a few questions on this $PATH in Linux.
I know it tells the shell which directories to search for executable files, so:
Upvotes: 10
Views: 55103
Reputation:
this is simple and i do like this way.
Open the linux bash shell and print the environment variables:
printenv
I copy "PATH
" variable to a text editor and edit as I want. Then update the PATH
like this
export PATH= /variable dir list/
It Works.
or if you want to add an single variable use this command.
export PATH = $PATH:/variable_dir_path/
This will extends the PATH with your new directory path.
Upvotes: 0
Reputation: 1440
To get your path current $PATH
variable type in:
echo $PATH
It tells your shell where to look for binaries.
Yes, you can change it - for example add to the $PATH
folder with your custom scripts.
So: if your scripts are in /usr/local/myscripts
to execute them you will have to type in a full path to the script: /usr/local/myscripts/myscript.sh
After changing your $PATH
variable you can just type in myscript.sh
to execute script.
Here is an example of $PATH
from RHEL:
/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/user/bin
To change your $PATH
you have to either edit ~/.profile
(or ~/.bash_profile
) for user or global $PATH
setting in /etc/profile
.
One of the consequences of having inaccurate $PATH
variables is that shell will not be able to find and execute programs without a full $PATH
.
Upvotes: 16
Reputation: 2729
Firstly, you are correct in your statement of what $PATH does. If you were to break it somehow (as per your third point), you will have to manually type in /usr/bin/xyz if you want to run a program in /usr/bin from the terminal. Depending on how individual programs work, this might break some programs that invoke other ones, as they will expect to just be able to run ls or something.
So if you were to play around with $PATH, I would suggest saving it somewhere first. Use the command line instruction
echo $PATH > someRandomFile.txt
to save it in someRandomFile.txt
You can change $PATH using the export command. So
export PATH=someNewPath
HOWEVER, this will completely replace $PATH with someNewPath. Since items in path are separated by a ":", you can add items to it (best not to remove, see above) by executing
export PATH=$PATH:newPath
The fact that it is an environmental variable means that programs can find out its value, ie it is something that is set about the environment that the program is running in. Other environmental variables include things like the current directory and the address of the current proxy.
Upvotes: 8