How to set PATH only in bash script temporarily?

it's simply not true. If you write a script and change the $PATH variable, the change live only in the script:

vi test.sh

inside the file:

#!/bin/bash

export PATH="$PATH:test"

let's test:

echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/home/matteo/.local/bin:/home/matteo/bin:./bin:/home/matteo/.local/bin:/home/matteo/bin:./bin

chmod ug+x test
./test 
echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/home/matteo/.local/bin:/home/matteo/bin:./bin:/home/matteo/.local/bin:/home/matteo/bin:./bin

same output. The change is effective only inside the script!


I think you are looking for "source" command (linux).

First write you set Path command (or whatelse) in file, like this loadMyEnvironment.sh

#!/bin/bash
export PATH="$PATH:test"
export MESSAGE="Now i am available on the current shell"

Now,type in the shell,

> source ./loadMyenvironment.sh

Now, in current shell you can check that your envs are loaded.

> echo $MESSAGE
Now i am available on the current shell

hope this helps.

Tags:

Bash