Linux/Environment Variables

From Wiki

Basics

  • List all environment variables
env
printenv
  • get variable
echo $USER
printenv USER
  • set variable
VARIABLE='Hello world'
echo $VARIABLE
  • promote a shell variable to an environment variable
export VARIABLE='Hello world'
  • variable lists
VARIABLE='Hello world':'test'    # variable with two elements
VARIABLE=$VARIABLE:'test2'       # add another element
  • remove variable
unset VARIABLE

Use in scripts

  • script.sh
DATE='01-01-2000'
export DATE
export VARIABLE="123"
  • load variables from .env file and export them
set -o allexport
source .env set
set +o allexport