How to run .sh using go lang programming on windows

test1.go

package main

import
(
“bytes”
“fmt”
“os/exec”
“strings”
“log”
)

func main()

{

    cmd := exec.Command("bash", "test.sh");                                                                                                                                                                    
    cmd.Stdin = strings.NewReader("");                                                                                                                                                                         
    var out bytes.Buffer;                                                                                                                                                                                      
    cmd.Stdout = &out;                                                                                                                                                                                         
    err := cmd.Run();                                                                                                                                                                                          
    if err != nil  {                                                                                                                                                                                            
            log.Fatal(err);                                                                                                                                                                                    
    }                                                                                                                                                                                                          
    fmt.Printf("Output : ", out.String());                                                                                                                                                                                                                                                                                                                                                                                      

}

test.sh

echo “Hello world”

My query

when trying to run .go file i.e., go run test1.go

i am getting “bash”:executable not found or %PATH% not found error

so plzz asap help me with this

Are you running this on Windows? %PATH% looks like it.

@lutzhorn yeahh i am running on windows but i am getting the error as i mentioned

Do you have bash installed on your Windows machine?

no can u help me with this

Then you need to install it. There are several ways.

On current windows using hyper v you can use the Linux 4 windows sub system, but you have to run your tool inside of it to make it work. You can’t mix windows and Linux sub systems.

Another way is to install cygwin or msys 2, both installations are not trivial.

@NobbZ
if all set up is good then …u r telling we can execute a .sh file using go lang without any issue

It is not Go that is executing your .sh file but the installed bash. Go is only calling bash as a suprocess using exec.Command.

1 Like

If everything is set up correctly, you ring them of course. But bash scripts in general live from running even more programms, those have to be installed on the computer as well, and they have to in a way that your bash does find them. Especially if you want to mix in software installed via windows installers outside of the bash’s simulated POSIX file tree, you get into even more configuration trouble.

To be honest, it might be a thousand times easier to translate that script to go if you have to be cross platform.

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