export
Usage
$ export KEY=VALUE
Or set a value first and export it later.
$ KEY=VALUE
$ export KEY
Export multiple values:
$ export KEY1=VALUE1 KEY2=VALUE2
See exported variables:
$ export
COLORFGBG='15;0'
COLORTERM=truecolor
HOME=/Users/mcurrin
HOMEBREW_NO_AUTO_UPDATE=1
...
That includes some system-set variables and some variables that I exported in my configs like ~/.bashrc
.
Examples
Here the NAME
variable is not set, so we default to using World
.
$ NAME=developer
$ bash -c 'echo "Hello, ${NAME}."
Hello, .
In the flow, we export a new variable.
$ export NAME=developer
$ bash -c 'echo "Subshell - hello, ${NAME}."'
Hello, developer.
$ echo "Outer shell - hello, $NAME."
Outer shell - hello, developer.
Or export an existing variable.
$ NAME=developer
$ export NAME
$ bash -c 'echo "Subshell - hello, ${NAME}."'
Subshell - hello, developer.
Why do I need export
Here is when you don’t need export
. If the variable is used in the same process.
$ NAME=developer
$ echo "Hello, $NAME!"
Hello, developer!
But if you run a subprocess, then that won’t be able to access the environment variables a level higher.
For example if you have Node or Python script which looks for NAME
on the environment variables. Or a shell script which uses $NAME
.
Here we in a Bash subshell, passing commands inline with -c
flag. Or we could have used a separate script and called it as bash script.sh
.
$ NAME=developer
$ bash -c 'echo "Hello, $NAME."'
Hello, .
Now we use export
to make the variable accessible in the subshell.
$ export NAME=developer
$ bash -c 'echo "Hello, $NAME."'
Hello, developer.
Functions
Note: If you are in ZSH, then first run bash -l
to make sure that you define and export the function in Bash, before using it in a Bash subshell.
foo () {
echo "Foo $1"
}
foo bar
# Foo
echo $(foo bar)
# Foo bar
Export and use it in a subshell:
$ export -f foo
$ bash -c foo bar
Foo
Example use case:
$ export -f foo
$ find . -name README.md -exec bash -c 'foo $0' '{}' \;
Foo ./docs/README.md
Foo ./README.md
Foo ./vendor/bundle/ruby/2.7.0/gems/sass-3.7.4/README.md
Export config file
See dotenv file cheatsheet for more info.
If you had values to set by hand, you could do this:
$ export FOO=bar FIZZ=buzz
But what those in are a file?
.env
FOO=bar FIZZ=buzz
Then you can do this, which reads the contents of the .env
shell file and passes the contents to export
to evaluate it.
$ export $(xargs< .env)
Using < file.txt
is a more efficient form of cat file.txt
. See Useless use of cat award.
Check the export
variables to see that it worked.
$ export | egrep '(FOO)|(FIZZ)'
FIZZ=buzz
FOO=bar
Breaking it down:
$ echo $(< .env)
FOO=bar FIZZ=buzz
$ xargs < .env
FOO=bar FIZZ=buzz
Evaluate:
$ export $(xargs< .env)
I find it works without xargs
sometimes but it is safer with, perhaps for multi-line values?
Warning - make sure to not have any comments in your file, as xargs
makes the output all on one line and you’ll lose anything after the comment.