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:
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?
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.
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.
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 ' !
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 inVAR=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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With