Setting variables with spaces within .env
Asked Answered
U

2

23

I have a .env file with a bunch of variables and I just came across an error.

One of the variables has spaces.

TEST="hello world"

So when I run this, learned about in this answer here.

env $(<.env)

I get this error.

env: world"': No such file or directory

How do I set variables with spaces within .env?

Unwise answered 2/4, 2015 at 21:29 Comment(4)
You just need to quote the file contents: env "$(<.env)"Zaragoza
@glennjackman env "$(<.env)" echo $TEST seems to print a blank line.Unwise
@glennjackman, @ThomasReggi: This cannot work with the echo example because the substitution of $TEST is done before running echo in the current shell which does not have the TEST variable set. When echo is run, TEST is set, but echo does not know what to do with $TEST.Klapp
It should work for external commands though and may be more feasible than my solution in that case.Klapp
K
13

If your command is just a shell command, you could run your command in a subshell like this:

( . .env ; echo "$TEST" )

The source or . builtin has no problem with assignments containing spaces. It will set the variables in the .env file in the current shell's environment.

In the more likely case of calling an external program, you'll also have to add 'export' to each assignment in your env file like this:

export TEST="hello world"

This is necessary because source does not export assigned variables as env does, i.e. they are set inside the subshell only but not in the environment of another process started inside that subshell.

Klapp answered 2/4, 2015 at 21:33 Comment(4)
I don't have a test.env file I don't understand what that is. Let's say my cmd is just to echo $TEST so from your example ( . .env ; echo $TEST ) does not work it gives me -bash: world: command not found.Unwise
Ah, sorry, I thought .env was just the extension. I'll modify.Klapp
"The source or . builtin has no problem with assignments containing spaces" is incorrect. Un-quoted spaces will cause bash to think the word after the space is a command, that's why OP is getting "command not found". The values after the = need to be quoted if they contain spaces.Lisp
Thank you for your feedback. Let me share my thoughts on that: (1) The space in the OP's example is quoted, so my statement is correct in that context. (2) As I noted in the question comments, just quoting the command substitution would probably be sufficient in most situations, but it would not work with the example provided by glennjackman. (3) The actual problem here is that the quotes read from .env in the command substitution are not interpreted by Bash, then word splitting occurs, and env interprets the second argument world" as a command (env NAME=value COMMAND ARG ...).Klapp
S
6

juste put the word that contains the space between " ".

Spellbinder answered 20/1, 2021 at 23:55 Comment(1)
If the value contains ", escape inner double-quote with \". For example, MESSAGE="Lets say \"Hi\""Hypophyge

© 2022 - 2024 — McMap. All rights reserved.