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 the 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/surendra-anne/scripts. I want to execute my scripts without 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/surendra-anne/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
Latest posts by Surendra Anne (see all)
- Docker: How to copy files to/from docker container - June 30, 2020
- Anisble: ERROR! unexpected parameter type in action:
Fix - June 29, 2020 - FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018