Linux command: export

Quick startexportCommand to export variables to the child process

ThisexportCommands are used to export variables to the child process.

What does it mean?

Suppose you define a variable TEST in this way:

TEST="test"

You can print its value using the following commandecho $TEST:

But if you try to define the Bash script in the filescript.shUse the above command:

Then you setchmod u+x script.shThen execute the script./script.sh, Thisecho $TESTAbout to print nothing!

This is because in BashTESTVariables are defined locally in the shell. When a shell script or other command is executed, a sub-shell program will be launched to execute the script, and the sub-shell program does not contain the current shell local variables.

In order to make the variable available, we need to defineTESTnot like this:

TEST="test"

But this:

export TEST="test"

Try it out, then run./script.sh"Test" should now be printed:

Sometimes you need to append some content after the variable. Usually throughPATHChanging. You use the following syntax:

export PATH=$PATH:/new/path

Commonly usedexportWhen you create a new variable in this way, also when you.bash_profileor.bashrcUse Bash's configuration file, or.zshenvWith Zsh.

To delete variables, use-nOptions:

export -n TEST

callexportIf there are no options, all exported variables will be listed.

ThisexportThis command can be used in Linux, macOS, WSL and wherever you have a UNIX environment

Download mine for freeLinux Command Manual


More cli tutorials: