Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 849
qodeninja Avatar asked Aug 19 '18 18:08

qodeninja


People also ask

Why do we use single and double quotation marks?

In US English, you must use double quotation marks. Single quotation marks are used for quotes within quotes. In UK English, it's most common to use single quotation marks, with double quotation marks for quotes within quotes, although the other way around is acceptable too.

Is there a difference between single and double quotes in Bash?

Single quotes won't interpolate anything, but double quotes will. For example: variables, backticks, certain \ escapes, etc. Enclosing characters in single quotes ( ' ) preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

What do double quotes mean in Bash?

Enclosing characters in double quotes (' " ') preserves the literal value of all characters within the quotes, with the exception of ' $ ', ' ` ', ' \ ', and, when history expansion is enabled, ' ! '. When the shell is in POSIX mode (see Bash POSIX Mode), the ' !


1 Answers

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

like image 159
melpomene Avatar answered Sep 30 '22 16:09

melpomene