Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

export, unset

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

Niveau
RHCSA-examen
Groep
Shell en hulp
Root nodig
Nee
Pakket
bash (built-in)
Ook
unset

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Wat het doet

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).

Zo schrijf je het

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

Opties 6

Meest gebruikt

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.

Voorbeelden 11

$ als gewone gebruiker# als root (of met 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.

Let op

  • 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.

Bestanden

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

Tips om te onthouden

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

Volledige handleiding

Deze pagina legt het commando uit in eenvoudige woorden. De originele handleiding heeft elk detail.

In je terminal: man export