我正在尝试使用Composer的带有引号的脚本将环境变量传递给将使用Docker运行的命令。
我使用sh -c 'A=b [command]'
来运行带有环境变量的命令。
这里有一个最小的例子:
{
"scripts": {
"docker-run": "docker run --tty composer:2",
"docker-version": "@docker-run composer --version",
"docker-version2": "@docker-run sh -c 'CONSTANT=6.2.x-dev composer --version'"
}
}
当我运行它时,脚本docker-version
按预期工作:
$ composer run-script docker-version
> docker run --tty composer:2 'composer' '--version'
Composer version 2.3.10 2022-07-13 15:48:23
但是脚本docker-version2
失败了。简单的引号被转义,它破坏了命令:
$ composer run-script docker-version2
> docker run --tty composer:2 'sh' '-c' ''''CONSTANT=6.2.x-dev' 'composer' '--version''''
composer: line 0: syntax error: unterminated quoted string
Script docker run --tty composer:2 handling the docker-run event returned with error code 2
Script @docker-run sh -c 'CONSTANT=6.2.x-dev composer --version' was called via docker-version2
您可以使用env
命令设置环境变量。
docker-run env CONSTANT=6.2.x-dev composer --version