84

set command displays all the local variables like below. How do I export these variables all at once?

>set
a=123
b="asd asd"
c="hello world"
5
  • what do you mean export all at once? you can use semi colons to define in one line...like a=123;b="asd asd";c="hello world"
    – Raza
    Commented Jun 11, 2013 at 20:57
  • Very similar to unix.stackexchange.com/q/79064/4667 Commented Jun 11, 2013 at 21:00
  • 2
    set also displays functions and system variables like BASH_VERSION Commented Jun 11, 2013 at 21:02
  • Your question is unclear. Is that an excerpt of set output you're showing? If so, then it's not bash's. Do you want to export all the currently set variable including the special shell variables? Or only those 3 variables like in export a b c? Commented Jun 11, 2013 at 21:07
  • export ${!T*} would export any defined parameter whose name starts with T. Unfortunately, there doesn't seem to be a way to easily generate a list of all defined parameters.
    – chepner
    Commented Jun 13, 2013 at 15:43

5 Answers 5

146

Run the following command, before setting the variables:

set -a
set -o allexport  # self-documenting version

man page:

-a
When this option is on, the export attribute shall be set for each variable to which an assignment is performed

-o option-name
Set the option corresponding to option-name:

  • allexport
    Same as -a.

To turn this option off, run set +a or set +o allexport afterwards.

Example:

set -a  # or: set -o allexport 
. ./environment
set +a

Where environment contains:

FOO=BAR
BAS='quote when using spaces, (, >, $, ; etc'
4
  • 17
    This must be enabled before assigning to variables, though. It doesn't do anything to previously assigned variables.
    – chepner
    Commented Jun 13, 2013 at 15:13
  • 1
    @chepner , Thanks i forgot to mention that !!
    – Nitin4873
    Commented Jun 13, 2013 at 15:41
  • 3
    This also automatically exports functions in the same way as function example(){ echo good; }; export -f example
    – Oliver I
    Commented Aug 29, 2017 at 15:16
  • Yeah, don't do this. Whitelist the variables that need to be exported to a subshell, which takes just a little more effort, but will save you a lot more effort, when you inevitably will need to track down errors caused by unintentionally overriding already defined envs. BAR=hello sh -c 'echo fu $BAR' _, otherwise someone will eventually do set -a; ...; AWS_ACCESS_KEY_ID=devkey.. ; aws something something broken Commented Apr 10 at 9:05
8

Same preliminary requirement as chosen answer ... either explicitly export each variable as per

export aaaa=1234

or prior to any variable assignment issue

set -a  #  for details see answer by @nitin 

then this works if your shell is bash ( possibly other shells as well )

export > /my/env/var/file

your new file will contain a dump of all currently defined variables ... with entries like

declare -x PORT="9000"
declare -x PORT_ADMIN="3001"
declare -x PORT_DOCKER_REGISTRY="5000"
declare -x PORT_ENDUSER="3000"
declare -x PRE_BUILD_DIR="/cryptdata6/var/log/tmp/khufu01/loud_deploy/curr/loud-build/hygge"
declare -x PROJECT_ID="hygge"
declare -x PROJECT_ID_BUSHIDO="bushido"

then to jack up current shell with all those env vars issue

source  /my/env/var/file
2
  • 1
    I believe export only prints variables that are already (marked to be) exported, while I believe the question is about variables that are set, but not marked to be exported. I just tested this on bash, which prints nothing: FOOBAR=x; export | grep FOOBAR. Commented Mar 9, 2022 at 15:58
  • @MatthijsKooijman good catch I updated to qualify my answer Commented Mar 10, 2022 at 12:07
5
`echo "export" $((set -o posix ; set)|awk -F "=" 'BEGIN{ORS=" "}1 $1~/[a-zA-Z_][a-zA-Z0-9_]*/ {print $1}')`
  1. First, get all set environment variables: (set -o posix ; set) Reference: https://superuser.com/questions/420295/how-do-i-see-a-list-of-all-currently-defined-environment-variables-in-a-linux-ba

  2. Get all environment variable names, separated by space: awk -F "=" 'BEGIN{ORS=" "}1 $1~/[a-zA-Z_][a-zA-Z0-9_]*/ {print $1}' Reference: awk-Printing column value without new line and adding comma and https://stackoverflow.com/questions/14212993/regular-expression-to-match-a-pattern-inside-awk-command

  3. Now, we need to export these variables, but xargs can not do this because it forks child process, export have to be run under current process. echo "export" ... build a command we want, then use `` to run it. That's all :p.

3
  • Welcomme to U&L SE. Maybe you can edit your post and give some explication.
    – Archemar
    Commented Mar 6, 2015 at 12:09
  • 1
    It's not correct to assume environment variable names will consist solely of a-z and A-Z. They commonly include underscores and digits as well, so the pattern would be [a-zA-Z_][a-zA-Z0-9_]*. There are some variations on this based on the shell you're using, but this is the safe / portable approach. Commented May 5, 2017 at 17:27
  • Good point @ChrisJohnson - updated! Commented Aug 29, 2017 at 15:52
3

You can prepend export to the variable name via awk and eval the resulting output:

eval $(printenv | awk -F= '{ print "export " $1 }')
1
  • 3
    printenv prints the variables that are already exported. That also won't work properly if there are variables that contain newline characters. Commented Aug 29, 2017 at 10:57
3

compgen -v will print a list of all variable names so you can export them all with

export $(compgen -v)

This will have various effects depending on the variables you have defined (ex: BASHOPTS will get exported by this). Be wary of how you use this.

2
  • Or just export $(compgen -v) (also assuming an unmodified $IFS), bearing in mind that exporting bash builtin variables such as SHELLOPTS or BASHOPTS can have nasty consequences. Commented Feb 6, 2022 at 15:01
  • Thanks, I never realized export accepts multiple symbols. I've incorporated most of your comment into the answer.
    – johncs
    Commented Feb 12, 2022 at 0:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .