Home > Blockchain >  Quotes in scripts results in “unterminated quoted string”
Quotes in scripts results in “unterminated quoted string”

Time:07-29

I'm trying to use scripts of Composer with quotes to pass an environment variable to the command that will be run with Docker.

I use sh -c 'A=b [command]' in order to run a command with an environment variable.

Here is a minimal example:

{
  "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'"
  }
}

When I run it, the script docker-version works as expected:

$ composer run-script docker-version
> docker run --tty composer:2 'composer' '--version'
Composer version 2.3.10 2022-07-13 15:48:23

But the script docker-version2 fails. The simple quotes are escaped and it breaks the command:

$ 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

CodePudding user response:

You can set environment variables with env command.

docker-run env CONSTANT=6.2.x-dev composer --version
  • Related