简体   繁体   English

为什么bash使用单引号和双引号?

[英]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. 我有一个Bash的情况,我以前从未遇到过,也不知道如何解决。 I installed bash on Alpine Linux (Docker Container) and for some reason environment variables with quotes translate literally. 我在Alpine Linux(Docker Container)上安装了bash,出于某种原因,带引号的环境变量按字面翻译。

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) bash-4.4# (路径已更改)


> 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. 我之前从未见过这个,因为我希望bash能够吞噬外部引号,甚至不确定在尝试解决这个问题时要搜索什么。

To fully qualify the scenario let me point out that: 为了完全符合这种情况,请允许我指出:

  1. Using Docker with an Alpine (3.8) image 使用Docker和Alpine(3.8)图像
  2. Installing Bash 4 on Alpine that usually defaults to ash shell 在Alpine上安装Bash 4,通常默认为灰壳

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 " => \\" . 我正在使用Docker Compose中env_file将环境变量推送到容器,它看起来像是字面上复制引号" => \\"

Thanks to @bishop's comment to try od -x 感谢@ bishop的评论尝试od -x

container.env container.env

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

Then inside the Alpine 3.8 container running env 然后高山3.8容器中运行的内部env

MY_PATH="/home/my/path"

Update 2 更新2

Looks like there was a bug around this that was closed. 看起来周围有一个关闭bug。 But apparently doesn't seem fixed. 但显然似乎并不固定。 Is it because I'm the only one in the universe still using Docker Toolbox? 是因为我是宇宙中唯一还在使用Docker Toolbox的人吗?

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

These syntax rules apply to the .env file: 这些语法规则适用于.env文件:

  • Compose expects each line in an env file to be in VAR=VAL format. Compose期望env文件中的每一行都是VAR=VAL格式。
  • 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 . 这意味着它们是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). 特别是,env文件不是shell脚本,bash看不到(你的#!/usr/bin/env bash行被视为注释而被忽略)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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