从包含在 makefile 中的 .env 文件中去除变量的引号

strip quotes from variable from .env file included in makefile

我有一个环境变量,里面有 space 个字符。有些工具不喜欢引用变量的值,因为它们会将引用视为变量的一部分。

这是在 .env 文件中设置的。

PIP_EXTRA_INDEX_URL="https://token@repo https://token@repo"

当我在 Makefile 中包含和导出此 .env 文件时,我收到此警告。

WARNING: Location '"https://token@repo' is ignored: 
it is either a non-existing path or lacks a specific scheme.

但我在其他工具中也看到了最初提到的这种行为。有办法处理吗?

在 Makefile 中,我将其包含如下。

include .env
export

build:
    docker build --build-arg PIP_EXTRA_INDEX_URL -t myimage .

Makefile 不是 shell 脚本,并且不可能在 shell 和 make 中使用相同的语法来定义变量,除非在非常有限的情况下。

在 shell 中,您可以在同一行上有多个作业,甚至可以在同一行上有 运行 个程序。所以,如果你的作业中有空格,你必须像你在这里所做的那样引用它。

在 make 中,赋值的语法是赋值后的所有文本(和前导空格)成为变量的值,不需要引用;看到的任何引号都保留为变量值的一部分。

所以,在 shell 这个作业中:

PIP_EXTRA_INDEX_URL='https://token@repo https://token@repo'

将 shell 变量 PIP_EXTRA_INDEX_URL 设置为值 https://token@repo https://token@repo ...请注意,引号被 shell.

从值中删除

在做这个作业:

PIP_EXTRA_INDEX_URL='https://token@repo https://token@repo'

将 shell 变量 PIP_EXTRA_INDEX_URL 设置为值 'https://token@repo https://token@repo' ...请注意引号 而不是 由 make 从值中删除.

因此,如果您在这样的食谱中使用此值:

do something "$(PIP_EXTRA_INDEX_URL)"

然后 make 将扩展该变量,您将得到:

do something "'https://token@repo https://token@repo'"

(包括引号)那是你的问题。

它是这样工作的。

build:
    docker build --build-arg PIP_EXTRA_INDEX_URL=$(PIP_EXTRA_INDEX_URL) -t myimage .