0

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.

1 Answer 1

3

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

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

readarray -d "" env_vars < <(env -0)
Sign up to request clarification or add additional context in comments.

2 Comments

./start.sh: line 7: syntax error: unexpected redirection shucks
@DeanHiller You are not using bash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.