6

I have a situation in Bash I've never encountered before and don't know how to resolve. I installed bash on Alpine Linux (Docker Container) and for some reason environment variables with quotes translate literally.

MY_PATH="/home/my/path"

> cd $MY_PATH

Result

bash: cd: "/home/my/path": No such file or directory


> echo $MY_PATH

Result

"/home/my/path"


Now if you try it without quotes it works

MY_PATH=/home/my/path

> cd $MY_PATH

Result

bash-4.4# (path changed)


> echo $MY_PATH

Result

/home/my/path


I've never seen this before as I expect bash to gobble up the outer quotes, not even sure what to search for in trying to resolve this.

To fully qualify the scenario let me point out that:

  1. Using Docker with an Alpine (3.8) image
  2. Installing Bash 4 on Alpine that usually defaults to ash shell

Update

This is starting to look like a docker issue. I'm using the env_file in Docker Compose to push environment variables to a container and it looks like its literally copying quotes " => \".

Thanks to @bishop's comment to try od -x

container.env

#!/usr/bin/env bash
MY_PATH="/home/my/path"

Then inside the Alpine 3.8 container running env

MY_PATH="/home/my/path"

Update 2

Looks like there was a bug around this that was closed. But apparently doesn't seem fixed. Is it because I'm the only one in the universe still using Docker Toolbox?

qodeninja
  • 10,946
  • 30
  • 98
  • 152

1 Answers1

7

https://docs.docker.com/compose/env-file/

These syntax rules apply to the .env file:

  • Compose expects each line in an env file to be in VAR=VAL format.
  • Lines beginning with # are processed as comments and ignored.
  • Blank lines are ignored.
  • There is no special handling of quotation marks. This means that they are part of the VAL.

In particular, the env file is not a shell script and not seen by bash (your #!/usr/bin/env bash line is treated as a comment and ignored).

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • Thanks I was just reading through that, looks like the real issue here is that literal translation of quotes is not (my) expected behavior. https://github.com/docker/compose/issues/3702 – qodeninja Aug 19 '18 at 19:02