Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

-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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading