What is this $PATH in Linux and how to modify it
What is this $PATH in Linux and how to modify it
I have a few questions on this $PATH in Linux.
I know it tells the shell which directories to search for executable files, so:
3 Answers
3
To get your path current $PATH
variable type in:
$PATH
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.
$PATH
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.
/usr/local/myscripts
/usr/local/myscripts/myscript.sh
$PATH
myscript.sh
Here is an example of $PATH
from RHEL:
$PATH
/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
.
$PATH
~/.profile
~/.bash_profile
$PATH
/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
.
$PATH
$PATH
and while you're at it can you also explain briefly what is this --> ~/.bashrc
– ruggedbuteducated
May 15 '13 at 8:52
@ruggedbuteducated just bash commands which are executed after you log in. Please look into man bash and search for bashrc.
– Chris
May 15 '13 at 8:56
Thanks Chris. I love you
– ruggedbuteducated
May 15 '13 at 8:59
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.
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
PATH
PATH
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.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Oh my God, you clarified it all for me by your statement --> "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." Thanks a lot
– ruggedbuteducated
May 15 '13 at 8:50