script icon indicating copy to clipboard operation
script copied to clipboard

Basename introduces a line feed

Open valrusu opened this issue 1 year ago • 1 comments

Hi

It seems Basename introduces a line feed in the generated path, hope I'm not doing anything wrong:

package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/bitfield/script"
)

func main() {

	myname1, err := script.Echo(os.Args[0]).Basename().String()

	if err != nil {
		fmt.Println(err)
	}

	myname2 := filepath.Base(os.Args[0])

	fmt.Printf("*%s*\n", myname1)
	fmt.Printf("*%s*\n", myname2)

}

Results:

*main3.exe
*
*main3.exe*

valrusu avatar May 12 '23 15:05 valrusu

That's right, @valrusu. It makes more sense when you think about a pipeline that contains multiple paths, one per line (which is the typical use case for script).

For example:

script.FindFiles(".").Basename().Stdout()

bitfield avatar May 12 '23 17:05 bitfield