Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

export, unset

Make a shell variable available to the programs you start (export), or remove a variable (unset).

Level
RHCSA exam
Group
Shell and help
Needs root
No
Package
bash (built-in)
Also called
unset

What it does

A variable you set in Bash (NAME=value) is only known to that shell. export NAME puts it in the environment, so every program and script started from this shell gets a copy.

unset NAME removes a variable (or a function with unset -f).

To keep a variable for future logins, write the export line in ~/.bashrc (for you) or in a file in /etc/profile.d/ (for everybody).

How to write it

export [-fn] [NAME[=VALUE] ...]
export -p
unset [-fv] NAME [NAME ...]

Options 6

Most used

NAME=VALUE
Set the value and export it in one step.
-p
List all exported variables (as export lines).
-n
Remove the export mark: the variable stays in the shell but is no longer passed on.
-f
The names are shell functions (export a function to child shells).

unset

-v
The names are variables (the default).
-f
The names are functions.

Examples 11

$ run as a normal user# run as root (or with sudo)

  1. 1Set and export a variable in one step

    export APP_ENV=staging
    bash -c 'echo $APP_ENV'
    staging

    The new shell (a child program) sees the value.

  2. 2Add a folder to the PATH

    export PATH="$PATH:$HOME/bin"

    Keep the old PATH and add your folder at the end. Put the line in ~/.bashrc to keep it.

  3. 3Keep a variable for every login

    echo 'export EDITOR=vim' >> ~/.bashrc

    Tools like crontab -e, visudo and git use the EDITOR variable.

  4. 4Set a variable for all users

    echo 'export HISTTIMEFORMAT="%F %T "' > /etc/profile.d/history-time.sh

    Every user gets times in history after the next login.

  5. 5Use a proxy for the tools in this shell

    export http_proxy=http://proxy.example.com:3128 https_proxy=http://proxy.example.com:3128

    curl, dnf and pip read these variables.

  6. 6Remove a variable

    unset http_proxy https_proxy

    Turns the proxy off for this shell again.

  7. 7List the exported variables

    export -p | head

    Same information as env, but in a form you could paste back into a shell.

  8. 8Stop exporting a variable but keep it

    export -n APP_ENV

    The value stays in this shell; new programs no longer get it.

  9. 9Export a function to a child script

    log() { echo "$(date +%T) $*"; }
    export -f log
    bash -c 'log "hello from child"'

    Child Bash processes can use the function.

  10. 10Pass settings to a container or a tool without editing files

    export KUBECONFIG=$HOME/.kube/staging.yaml
    kubectl get nodes

    One variable switches the cluster for this terminal only.

  11. 11Remove a secret from the shell after use

    export DB_PASSWORD='s3cret'; ./migrate.sh; unset DB_PASSWORD

    Shorter life for the secret in the shell. Better: let the tool read it from a protected file.

Watch out

  • No spaces around =: export A = 1 is wrong.
  • A child can never change the variables of its parent. A script that exports a variable does not change your shell, unless you run it with source.
  • unset PATH breaks the shell until you set PATH again.

Files

~/.bashrc
Your exported variables for every shell.
/etc/profile.d/*.sh
Exported variables for all users.

Tips to remember

  • Set: NAME=value. Pass on: export NAME. Both: export NAME=value. Remove: unset NAME.