How to execute a cmd command and print response?

Hello i want to execute a cmd or shell command that return nodejs version or any other cmd command, how to do it i have this code

import (

func main(){
	output, err := exec.Command("cmd", "node -v").CombinedOutput()
    if err != nil {

what i spect “v14.12.0”

what i get : “Microsoft Windows [Versi�n 10.0.19042.928]”

I think you want to add the /c switch to cmd.exe:

	output, err := exec.Command("cmd", "/c", "node -v").CombinedOutput()
1 Like
cmd := "node -v"
output, _ := exec.Command("bash", "-c", cmd).Output()

And you can put long, even multi-command commands in cmd, e.g

cmd := fmt.Sprintf("cd %s; tar czf %s.tgz *.log; tar tvf %s.tgz", path, taskId, taskId)
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.