19

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?

Community
  • 1
  • 1
ThomasReggi
  • 55,053
  • 85
  • 237
  • 424
  • 1
    You just need to quote the file contents: `env "$(<.env)"` – glenn jackman Apr 02 '15 at 21:33
  • 1
    @glennjackman `env "$(<.env)" echo $TEST` seems to print a blank line. – ThomasReggi Apr 02 '15 at 21:39
  • @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`. – Michael Jaros Apr 02 '15 at 22:00
  • It should work for external commands though and may be more feasible than my solution in that case. – Michael Jaros Apr 02 '15 at 22:03

2 Answers2

9

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.

Michael Jaros
  • 4,586
  • 1
  • 22
  • 39
  • 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`. – ThomasReggi Apr 02 '15 at 21:38
  • Ah, sorry, I thought `.env` was just the extension. I'll modify. – Michael Jaros Apr 02 '15 at 21:40
  • "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. – Isaac Freeman May 02 '18 at 17:04
  • 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 ...`). – Michael Jaros May 02 '18 at 18:33
6

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

Michael Jaros
  • 4,586
  • 1
  • 22
  • 39
  • 1
    If the value contains `"`, escape inner double-quote with `\"`. For example, `MESSAGE="Lets say \"Hi\""` – Qinjie Feb 17 '22 at 01:45