How to pipe bash script output to a file on windows

5.5k views Asked by At

I am running on Windows 7.

I have a bash script which runs fine from a Windows command prompt using "C:\Program Files\Git\git-cmd.exe" "C:\path\myBashScript.sh" 17.1. I can see it running in a new bash window, the output looks fine and it closes. What I want to do is capture the output in a file for further processing.

I tried

"C:\Program Files\Git\git-cmd.exe" "C:\path\myBashScript.sh" 17.1 > C:\out\myBashScript.out.txt

but all I get in the output is the working folder, ie, C:\path>

Is this possible?

Thanks

2

There are 2 answers

0
max630 On

"C:\Program Files\Git\git-cmd.exe" is a frontend windows launcher. I'm surprised it takes any argument at all. You should run bash scripts using bash interpreter:

"C:\Program Files\Git\bin\bash.exe" "C:\path\myBashScript.sh"

PS: I'd advise to apply redirection inside the bash script rather than in windows commandline, they are better controlled there.

2
Philip Oakley On

You need to discriminate between the outputs generated within the shell script, and those generated by the command line execution.

To easily capture the output of the shell script, you may find it easier to do the redirection > to the capture file within the script.

You didn't say what style of system you were on, and how it was set up (Cygwin, GfW; command line in cmd.exe, or bash; integrated or separated git commands (in the cmd.exe), etc). I suspect that the inability to do a simple redirection of the command line is because of that system setup issue.