Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exporting PATH not working in a bash shell on linux

Tags:

linux

bash

shell

For example, I type to the following command:

         # PATH=$PATH:/var/test
         # echo $PATH
           ........./var/test       // working
         # export PATH

Next, I open another bash shell session to test if the export works by typing the following command:

         # echo $PATH
          ........      // not working as in I don't see /var/test path
like image 825
Yet A-beyene Avatar asked Jul 20 '26 07:07

Yet A-beyene


1 Answers

you have set the PATH environment variable only for your current bash session. You need to add the line PATH=$PATH:/var/test into ~/.bashrc so that it works for any bash shell.

Just run the following command to put it into your rc(run commands) file (rc files contain startup information for a command(initialization)):
echo "PATH=$PATH:/var/test" >> ~/.bashrc

More info: https://en.wikipedia.org/wiki/Run_commands
https://superuser.com/questions/789448/choosing-between-bashrc-profile-bash-profile-etc

exporting a variable makes it available only in child processes spwaned/started from that bash shell.
As an example:

$ export var=abcd
$ sh
$ echo "$var"
abcd
$ exit
$ echo "$var"
abcd
$

sh is the child process of bash hence it gets the value of var, since you open a new bash which is a different process altogether it does get the PATH value.

like image 157
riteshtch Avatar answered Jul 22 '26 01:07

riteshtch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!