How to set the $PATH variable in linux

If you installed the daemon/service into default directories, it will be executed anywhere because the executable files are placed to “/usr/local/bin”.

If you installed the daemon/service into different directories, you need to add the path to “.bash_profile” file to execute anywhere, you can add the path to root or particular user, if you want to execute it by root you should add the path to “/root/.bash_profile” file.

I have installed the mysql and zendserver to different directories, So now i’m going to add those directories to “.bas_profile” file to execute anywhere.

Why to set PATH variable ?

PATH variable is system variable used to store executable path, if we set the path variable to daemon, we can simply type the daemon to execute it or else we need to type the exact/full path to execute the daemon.

By default the below path is added to executable path/location. Use the below command to print the path’s list.

# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

The “.bash_profile” file look like below.

# nano .bash_profile
  GNU nano 1.3.12                                         File: .bash_profile

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

I have run the both daemon before adding “.bash_profile” file and get below error message but i can able to run the exact path.

# zendctl.sh restart
-bash: zendctl.sh: command not found

mysql output error.

# mysql
-bash: mysql: command not found

This was successfully restarted at full path, see below output.

# /u01/zend/bin/zendctl.sh restart
/u01/zend/bin/apachectl stop [OK]
/u01/zend/bin/apachectl start [OK]
Stopping Zend Server GUI [Lighttpd] [OK]
spawn-fcgi: child spawned successfully: PID: 15921
Starting Zend Server GUI [Lighttpd] [OK]

Now, i’m going to add the mysql and zendserver path into “.bash_profile” file.

# nano .bash_profile
  GNU nano 1.3.12                                         File: .bash_profile

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH=$PATH:/u01/mysql/bin

export PATH=$PATH:/u01/zend/bin

To invoke .bash profile changes, use below commands.

. ./.bash_profile
or
source .bash_profile

Now, i can see the newly added path’s on our server

# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/u01/mysql/bin:/u01/zend/bin

Now, its successfully restarted.

# zendctl.sh restart
/u01/zend/bin/apachectl stop [OK]
/u01/zend/bin/apachectl start [OK]
Stopping Zend Server GUI [Lighttpd] [OK]
spawn-fcgi: child spawned successfully: PID: 16882
Starting Zend Server GUI [Lighttpd] [OK]

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

Leave a Reply

Your email address will not be published. Required fields are marked *