Home > other >  How to get environment variables into a bash array?
How to get environment variables into a bash array?

Time:11-25

In bash, how can I get all environment variables into an array variable?

I tried the following:

readarray -d "" env_vars <<< "$(env -0)"

This fails with

-bash: warning: command substitution: ignored null byte in input

Note that an environment variable may contain a multi-line string. Therefore splitting on EOL does not give the desired result.

CodePudding user response:

$(...) expands to a string, and strings can't have zero bytes.

Do not use <<<$(...), when you can use < <(...).

readarray -d "" env_vars < <(env -0)
  • Related