I have a few questions on this $PATH in Linux.
I know it tells the shell which directories to search for executable files, so:
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With