Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this $PATH in Linux and how to modify it

Tags:

linux

path

I have a few questions on this $PATH in Linux.

I know it tells the shell which directories to search for executable files, so:

  1. What does it mean an environmental variable?
  2. How to change its path? and is it recommended to change it?
  3. IF i change it what are the consequences?
like image 342
ruggedbuteducated Avatar asked May 15 '13 08:05

ruggedbuteducated


2 Answers

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.

like image 187
Chris Avatar answered Oct 14 '22 13:10

Chris


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.

like image 25
rspencer Avatar answered Oct 14 '22 12:10

rspencer