I'm trying to have a bash script create a variable and have the variable persist in the terminal until the terminal closes (not permanently). I thought export was what I needed but it didn't work.


export VARIABLE='this is a test'

Then run the script:

$ ./test.sh

Then in the same terminal:

$ echo "$VARIABLE"

But it produces a blank response.

  • A note on executable shell script names: Don't end file name with .sh: User should not need to know the language, and the language can change. – ctrl-alt-delor Dec 24 '18 at 11:15

What happened

You start a new process, set and environment variable (in the process), and exit the process. The variable is gone.

What to do

Source the bash script, don't execute it. e.g. one of these

source ./test.sh 
. ./test.sh

When you run the script you create subshell where the variable is set. After end of the script execution the variable is destroyed. If you want to have it in current shell execute the script on this way:

. ./test.sh

In addition to the source (or .), you might need to use a shell function instead.

Give a try to this:

test_sh() {
  export VARIABLE='this is a test'


printf "VARIABLE=%s\n" "${VARIABLE}"


printf "VARIABLE=%s\n" "${VARIABLE}
VARIABLE=this is a test

Not the answer you're looking for? Browse other questions tagged or ask your own question.