Thursday, December 18, 2014

How to set path in Linux ?



What is a path?
A path is a unique location to a file or a folder in a file system of an OS. A path to a file is a combination of / and alpha-numeric characters.

Why to set PATH variable?
PATH variable is system variable or environment variable used to store binaries/commands location/folder so that we no need to type complete path to execute the commands/binaries. If the PATH variable is not set you may get errors like “Command not found”. PATH variable can store multiple folders names with : as separator.

How to see PATH variable value?
$echo $PATH
 
How to set PATH variable?
Setting the path in bash or ksh or csh are one and the same. At the command prompt type below command to add new_folder_contain_binaries. 

PATH=$PATH:new_folder_contain_binaries
Note: We have to retain previous PATH values to make sure all the commands work without any issue. That is the reason We given $PATH in front of new path. 

Example1: You just install java and want to set path to java executables or binaries stored in /usr/share/java
PATH=$PATH:/usr/share/java
 
Example2: I have my scripts located in /home/linux/scripts. I want to execute my scripts with out running sh or bash before executing it.
For this we can set the path so that your shell scripts will now run as a shell script.
PATH=$PATH:/home/linux/scripts
Once we set the path to our scripts we no need to run bash scriptname or ksh scriptname we can just write scriptname to execute it.

How to set PATH variable permanently?
When you set path variable as mentioned above it will just set the PATH variable for that session. If you want to keep this PATH variable after reboots too then you have to set it permanently. 

For single user:
Edit ~/.profile(for KSH shell ) or ~/.bashrc (for Bash shell) for adding PATH variable in it as shown below. 

export PATH=$PATH:/usr/share/java
Save the file and exit
once the PATH variable is set, we have to source these files to make this new PATH available, otherwise we have to reboot the machine if you don't want to source it. 

source ~/.profile
or
source ~/.bashrc
 
Some of the applications we have to set the path for them to work properly. We will see this in other post on how to set different ENV variables for different applications

if u like this topic please share on google+

No comments:

Post a Comment