Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting variables with spaces within .env

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?

like image 518
ThomasReggi Avatar asked Apr 02 '15 21:04

ThomasReggi


People also ask

How do I set an environment variable with spaces?

The command is set and the parameter is variable=value . As for most commands and applications it is possible and often required to surround a parameter with double quotes if containing 1 or more spaces or any other character from this list: &()[]{}^=;! '+,`~ .

Can I use variables in .env file?

The . env file contains the individual user environment variables that override the variables set in the /etc/environment file. You can customize your environment variables as desired by modifying your . env file.

Can environment variables have dots?

It is not possible to set environment variables with dots in their name. If you try to set one in the UI, it just does not work without error message.


2 Answers

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.

like image 170
Michael Jaros Avatar answered Sep 30 '22 13:09

Michael Jaros


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

like image 22
fayssall allaoui Avatar answered Sep 30 '22 13:09

fayssall allaoui