# PATH Environment Variable in Linux/Unix

Table of Contents
What is the $PATH environment variable?Link to heading

Every Linux & Unix System has several environment variables which are dynamic variables essential for running several processes in the system. HOME,HOME,env being some of the well known environment variables.

The $PATH environment variable stores all the paths where one can find the binary executables for all the commands that we use.

Whenever a command is invoked from the terminal,all the paths in the $PATH environment variable are looked up and if the binary is found, the command is executed.

Trying this out on the terminal produces:

Terminal window
echo $PATH
/home/linus/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

The path variable can be usually edited or updated from the .bash_profile hidden file(On Unix like OS) or directly by exporting the variable with the export command.

Creating your own command recipies

Linux or Unix commands can be usually built by shell scripts, Python or Perl scripts. Let us create a simple Bash script that calculates the factorial of the number sent to it as an argument in the command.

#!/bin/bash
count=$1
fact=1
while [ $count -gt 0 ]
do
fact=$(( $fact * $count ))
count=$(( $count - 1 ))
done
echo $fact

Create a sample file with a .sh extension and copy the factorial code as given above. Add the directory which contains the given file to your $PATH variable. Edit the variable from your .bash_profile or .profile located in your root directory with your preferred text editor.

Terminal window
nano ~/.bash_profle

The file may contain several other aliases and variables but look out for the lines and update as shown below (Make sure there are no spacings between PATH and =)

Terminal window
export PATH=/home/linus/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/directory_of_bash_script

Do not forget to source the .bash_profile or .profile file with the source command or .

Terminal window
source .bash_profile

Calling the script file name from the terminal with any number as the argument to the command will print the factorial to stdout. The same thing can also be done by creating a symbolic link which will be discussed later.

My avatar

Thanks for reading my blog post! Feel free to check out my other posts or contact me via the social links in the footer.


More Posts