Skip to content

os/exec: passing single quotes to cmd.Env creates some weird escaping #26823

@drake7707

Description

@drake7707

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.10.3 linux/amd64

Does this issue reproduce with the latest release?

yes, with 1.10.3

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/root/go"
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build636672150=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Passing in cmd.Env environment variables with single quotes get mucked up

Repro:

test.sh:

#!/bin/bash

echo "Testing $DIND_REGISTRY"
set

test.go:

package main

import (
        "bytes"
        "fmt"
        "io"
        "os"
        "os/exec"
        "log"
)

func main() {

        cmd := exec.Command("test.sh")
        cmd.Env = os.Environ()

        test := `DIND_REGISTRY='[ "docker.io", "192.168.254.1:5000" ]'`

        fmt.Println("Appling env var " + test)
        cmd.Env = append(cmd.Env, test)

        var stdBuffer bytes.Buffer
        mw := io.MultiWriter(os.Stdout, &stdBuffer)

        cmd.Stdout = mw
        cmd.Stderr = mw

        if err := cmd.Run(); err != nil {
                log.Fatal("Unable to run the script:" + err.Error())
        }
}

What did you expect to see?

I expected to see the literal environment variable as I specified it in the script:
DIND_REGISTRY='[ "docker.io", "192.168.254.1:5000" ]'

What did you see instead?

DIND_REGISTRY=''\''[ "docker.io", "192.168.254.1:5000" ]'\'''

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions