How to get environment variables into a bash array?

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.

>Solution :

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

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

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

Leave a Reply