2

How can I export an environment variable like this?

USERS=[{name:a,surname:b,age:c},{name:d,surname:e,age:f}]

What I've tried so far unsuccessfully

[{"name":"a","surname":"b","age":"c"},{"name":"d","surname":"e","age":"f"}]
[{'name':'a','surname':'b','age':'c'},{'name':'d','surname':'e','age':'f'}]
[{\'name\':\'a\',\'surname\':\'b\',\'age\':\'c\'},{\'name\':\'d\',\'surname\':\'e\',\'age\':\'f\'}]
[{\"name\":\"a\",\"surname\":\"b\",\"age\":\"c\"},{\"name\":\"d\",\"surname\":\"e\",\"age\":\"f\"}]
"[{"name":"a","surname":"b","age":"c"},{"name":"d","surname":"e","age":"f"}]"
'[{"name":"a","surname":"b","age":"c"},{"name":"d","surname":"e","age":"f"}]'
'[{'name':'a','surname':'b','age':'c'},{'name':'d','surname':'e','age':'f'}]'

I know that with docker-compose and terraform this can easily be done but I have to define a single env var here

Something very important that I forgot to mentioned: I want this variable to be read as a LIST since it's part of a configuration file. Not as a string. Since I want to map it to a User object.

User {
   name,
   surname,
   age
}
3
  • Aside: Don't use all-caps names for user-defined variables; that's the namespace used for variables that either are used for the shell to inform applications about its environment, or for applications to modify shell (and other POSIX-specified tools') behavior. See pubs.opengroup.org/onlinepubs/9699919799/basedefs/…, keeping in mind that modifying a shell variable with the same name as a preexisting environment variable automatically updates the environment variable as well. Commented Sep 7, 2021 at 20:20
  • BTW -- you say you tried '[{"name":"a","surname":"b","age":"c"},{"name":"d","surname":"e","age":"f"}]', but you don't say how it failed, or otherwise provide the details of what this attempt looked like with enough specifics for anyone else to reproduce the failure. Commented Sep 7, 2021 at 20:21
  • 3
    All environment variables are strings -- every single one of them. You cannot have an environment variable that is not a string. You can make the code that receives or reads it interpret it as something else, but that's something the code that reads the variable has to do; the variable is always a string. Saying you want an environment variable but it can't be a string is self-contradictory. Commented Sep 7, 2021 at 20:30

1 Answer 1

4

Put it in quotes and use the export command to put it in the environment. Also, make it valid JSON by quoting all the strings

export USERS='[{"name":"a","surname":"b","age":10},{"name":"d","surname":"e","age":35}]'
Sign up to request clarification or add additional context in comments.

3 Comments

I want this variable to be read as a LIST since it's part of a configuration file. Not as a string. Since I want to map it to a User object.
Environment variables can only be strings.
you can use jq to parse it in the code that uses the environment variable.

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.