bashenvironment-variables

Linux environment variables setting disappear


I've got a following Bash script:

#!/bin/bash
export PYCACHED_HOST='localhost'
export PYCACHED_PORT=8001
echo "PyCached environment variables set to: PYCACHED_HOST=`echo $PYCACHED_HOST`, PYCACHED_PORT=`echo $PYCACHED_PORT`"

when I run it, following output is printed:

PyCached environment variables set to: PYCACHED_HOST=localhost, PYCACHED_PORT=8001

and when I run the same echo line again:

echo "PyCached environment variables set to: PYCACHED_HOST=`echo $PYCACHED_HOST`, PYCACHED_PORT=`echo $PYCACHED_PORT`"

I get:

PyCached environment variables set to: PYCACHED_HOST=, PYCACHED_PORT=

I know that environment variables are set for the script context, but what can I do to make them available after the script execution is over?


Solution

  • what can I do to make them available after the script execution is over?

    Run your script as;

    source ./script.sh
    

    OR

    . ./script.sh
    

    This will run your script in current shell without creating a new process hence environment variables will be available in current shell after script is over.