In most POSIX‐style shells (bash, zsh, etc.), including the ones you’ll find on macOS, set
can be used to turn various shell options on or off.
set -a
This turns on the allexport option (same as set -o allexport
). From that point on, every variable you assign is automatically exported into the environment.
$ set -a
$ FOO=bar # automatically exported
$ export -p # you’ll see FOO in the list of exported vars
set +a
This turns allexport back off (same as set +o allexport
). Variables you assign after this will not be exported unless you explicitly use export
.
xxxxxxxxxx
$ set +a
$ BAZ=qux # not exported
$ export -p # BAZ won’t be in the exported list
A common use-case is when you have a plain text file of variable assignments:
xxxxxxxxxx
set -a
source my-env-file.sh
set +a
That way everything defined in my-env-file.sh
ends up in your environment without having to prefix each line with export
.