简体   繁体   中英

Why is bash using single and double quotes literally?

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?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM