Linux – Windows command prompt capture output of bash script in one step

bashcommand linelinuxshellwindows

I have a bash script postCloneSetup.sh:

script_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
cd "$script_path"

printf 'Updating submodules for project\r\n'
git submodule update --init --recursive

printf '\r\nInitializing git hooks\n'
./GitHooks-BackEnd/init-hooks

printf '\r\nInitializing Submodule1\r\n'
$(./Submodule1/postCloneSetup.sh)

printf '\r\nInitializing Submodule2\r\n'
$(./Submodule2/postCloneSetup.sh)

I trigger a bash script from Windows command prompt.

postCloneSetup.sh

It opens another window and then returns. The window it spawned stays open and logs output text.

enter image description here

I want to capture the output from the spawned window (the text written to the console) and return that to the Windows command prompt.

I would prefer to use something like

$(postCloneSetup.sh) // Linux for capturing output to current context

for the Windows command prompt.

I'd prefer not to modify postCloneSetup.sh. I know I could have it write out to a file with

exec &> postCloneSetupLog.log

but then I must wait and manually run

type postCloneSetupLog.log

to see the output in the console. This is not possible for integrating into a CI engine, which is my goal.

How can I capture the output from the spawned console in one command?

Best Answer

Short answer:

To use Git Bash, and execute

  • "%ProgramFiles%\Git\bin\bash.exe" script_name.sh to run a script
  • "%ProgramFiles%\Git\bin\bash.exe" -c "echo 'it works'" to run one-liner

A context:

I came across this question when looking for a solution for something similar. In our set up we use Git Bash on Windows (specifically at the moment "GNU bash, version 4.4.19(1)-release (x86_64-pc-msys)") as a part of development environment for Windows, we have some scripts that we want to test via GitLab CI exactly in this environment: to execute them under Git Bash, and see the output in CI job.

What worked for us is to execute "%ProgramFiles%\Git\bin\bash.exe" script_name.sh where "script_name.sh" is a script to be executed by bash (or if you need to execute just inline command "%ProgramFiles%\Git\bin\bash.exe" -c "echo 'it works'"). An output of this script is visible in CMD window, and is captured by CI job.

May be a similar result can be achieved with Windows Subsystem for Linux, but as Git Bash was our requirement, we didn't investigate how it could be done with WSL.

So, a solution/workaround that I would propose from my experience is to install "Git For Windows" https://gitforwindows.org/ (Git Bash is a part of it), and run bash scripts via Git Bash as described above.

Topic Starter mentioned that running a script from CI was exactly his goal (quote: but the other "script" is just running it from the Windows command prompt. In the CI engine that is what is happening, link), and that was our use case as well, so I'm adding an example of CI job configuration (GitLab CI in our case, ".gitlab-ci.yml"). It is important to get all escaping correct to make it work, and it took us a couple of errors to do it right:

test_on_windows:
  stage: test
  script:
    - "\"%ProgramFiles%/Git/bin/bash.exe\" script_name.sh"
    # or
    - "\"%ProgramFiles%/Git/bin/bash.exe\" -c \"echo 'it works'\""
  tags:
    - windows
Related Question