Linux-Noob Forums

Full Version: Setting the path
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

This article explains how set your PATH variable under Linux. This has the same use as that of setting the PATH variable under DOS. Under Linux too, modifying the PATH would add these new directories to your default search path.

So in case you have a particular executable in a particular directory, then if you add that directory to your PATH, then you would only have to type the name of the executable at the prompt rather than then absolute path for that executable. Got it?? Read the example below to figure out what exactly I am speaking about..

 

 

Assumption

 

 

Suppose you have a program by the name 'tetris' in a folder called /usr/local/games . So in order to run this program you would have to type the following at the prompt

$/usr/local/games/tetris

 

The above command would execute your program. But typing this every time you want to play this wonderful game makes it slightly cumbersome. It would be much better if you could only type 'tetris'.

 

 

Solution

 

A solution would be to add the /usr/local/games directory to your PATH, so that next time onwards you would only have to type 'tetris' at the prompt rather than the absolute path.

 

To add this directory to your PATH you have to edit a file called 'bash_profile' that would be present in your Home directory (in Redhat Linux 6.2). So if there is a user by the name David then this file would mostly be found at /home/David/.bash_profile

 

Note : The period (.) before the name of the file. This period make this file a hidden file. So remember to view hidden files also while seeing a directory listing (This option would be in some menu in X , at the prompt simply use 'ls -a' to see hidden files).

 

This file would be having a particular line starting with the string PATH. For e.g. the file that I have on my machine has a line such as

PATH=/optional/bin:$PATH:$HOME/bin

 

To add the directory /usr/local/games to this I would have to modify this line as follows

PATH=/usr/local/games:optional/bin:$PATH:$HOME/bin

 

Once you have modified this file, save it and then execute it as follows

. $HOME/.bash_profile

 

Note : To execute this script basically at the $ prompt type a period ' . ' leave a space and then type $HOME/ Once this is done press <Tab> key. Doing so would replace what ever you have typed with the path to your home directory. Once this happens all you have to do is append a .bash_profile to what is already present at the prompt and finally press <Enter>

 

On executing the script you wouldn't see any messages at the output, but then onwards you could simply type 'tetris' at the prompt to execute the program /usr/local/games/tetris

 

So now you are on the right PATH ;-)


Quick and easy alias to view your current path is this:

 



Code:
alias path='echo -e ${PATH//:/\\n}'




 

Either execute this on the command line of add it into you .bashrc file.

 

Once thats done simply type path to view the output.