Skip to content

String method return a new line when followed by Exec #161

@ifraixedes

Description

@ifraixedes

Calling String followed by Exec returns a string ending with a new line (i.e. \n).

Example:

out, err := script.Exec("echo hello").String()
if err != nil {
	log.Fatal(err)
}

fmt.Printf("%q\n", out)

It prints: "hello\n"

Is this intended or the \n shouldn't be there?

If I'm not mistaken the issue is that os/exec.Command returns it. Execute this example

package main

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

  func main() {
    out := &bytes.Buffer{}

    cmd := exec.Command("echo", "hello")

    cmd.Stdout = out
    err := cmd.Start()
    if err != nil {
      log.Fatal(err)
    }

    cmd.Wait()

    fmt.Printf("%q\n", out.String())
  }

And you see that it prints: "hello\n"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions