How to Use Git Bash in Windows to Run Bash Scripts with PowerShell

If you work in a mixed environment with both Windows and Unix-like systems, you might already be familiar with Git Bash and PowerShell. Combining the two can be powerful for running bash scripts on Windows while taking advantage of PowerShell’s features. This post will show you how to effectively use Git Bash and PowerShell together.

Setting Up Git Bash on Windows

First, you need to have Git Bash installed on your Windows machine. If you haven’t done that yet, follow these steps:

  1. Download Git for Windows: Visit https://git-scm.com/downloads and download the installer for Windows.
  2. Install Git: Run the installer and make sure to select “Git Bash Here” during the installation, which will add Git Bash as an option when you right-click in Windows File Explorer.

With Git Bash installed, you can run bash commands on your Windows system, which is great for managing projects and scripting. However, what if you also want to incorporate PowerShell commands into your workflow? That’s where the real power of combining Git Bash with PowerShell comes in.

Running Bash Scripts with PowerShell

Step 1: Opening PowerShell and Git Bash

You can start by opening PowerShell or Git Bash, depending on where you’re most comfortable. If you’re in PowerShell but want to run bash commands, follow these steps:

  1. Open PowerShell: Press Win + X and select Windows PowerShell.
  2. Run Git Bash from PowerShell: Type bash and press Enter. This will drop you into a bash shell inside PowerShell, allowing you to run any bash commands.
PS C:\Users\YourUser> bash

After executing the above command, you’ll see a bash prompt. Now, you can execute bash scripts from within PowerShell.

Step 2: Running a Bash Script from PowerShell

To run a bash script from PowerShell, you can call Git Bash directly and pass the script to it. Suppose you have a script called my_script.sh that you want to run from PowerShell:

PS C:\Users\YourUser> C:\Program\ Files\Git\bin\bash.exe -c "./my_script.sh"

In this command:

  • C:\Program Files\Git\bin\bash.exe is the path to the bash executable installed by Git.
  • -c allows you to run a command in bash.
  • "./my_script.sh" is the script you want to run.

Step 3: Combining Bash and PowerShell Commands

One great use case for combining Git Bash and PowerShell is taking advantage of the best of both worlds. For instance, you could use PowerShell’s ability to manipulate the Windows environment and Git Bash’s command line utilities.

Here’s an example of running both in a single PowerShell script:

# Define a path variable in PowerShell
$Path = "C:\Users\YourUser\Documents"

# Use Git Bash to list files in that directory
C:\Program Files\Git\bin\bash.exe -c "ls '$Path'"

In this example, you set a PowerShell variable ($Path), and then use Git Bash to list the files in that directory. Notice that we use single quotes around $Path inside the bash command to ensure PowerShell passes the variable correctly.

Step 4: Creating an Alias for Convenience

Typing the full path to bash.exe can get cumbersome. You can make it easier by creating an alias in PowerShell to call Git Bash more conveniently:

# Create an alias for Git Bash
Set-Alias -Name bash -Value "C:\Program Files\Git\bin\bash.exe"

Now, whenever you want to run a bash script, you can just type bash in PowerShell:

PS C:\Users\YourUser> bash -c "./my_script.sh"

Practical Example: Automating Git Tasks

A common use case for combining Git Bash and PowerShell is to automate Git tasks, like creating backups of repositories.

Here is an example script that you can run from PowerShell to create a compressed archive of a Git repository using both PowerShell and Git Bash:

# Define repository path and backup file name
$RepoPath = "C:\Users\YourUser\Projects\MyRepo"
$BackupPath = "C:\Users\YourUser\Backups\MyRepoBackup.tar.gz"

# Run Git Bash to create a tar archive of the repository
bash -c "tar -czvf '$BackupPath' -C '$RepoPath' ."

This script sets the paths in PowerShell and then uses tar in Git Bash to create a compressed backup of the repository.

Embedded Example: Bash Script for Data Extraction

Here’s an embedded example of a bash script that extracts data from a .trj file, which can be used in both Windows and Linux environments:

#!/bin/bash

#exec='../../Code/Dyngraph/Release_gnu_l/dyngraph'
exec='../../Code/Dyngraph/Release_intel_w64/dyngraph.exe'

infile='obs.trj'
outfolder='./results/'

echo -e "$infile\nSP\nDE599G1\nSP\nDE592G1\nSP\nDE594G1\nSP\nDE598G1\nSP\nDE59DG1\nSP\nDE595G1\nSP\nDE5A1G1\nSP\nDE5A3G1\nSP\nDE59BG1\nSP\nDE59EG1\nSP\nDE5A0G1\nSP\nDE593G1\nSP\nDE596G1\nSP\nDE597G1\nSP\nDE5A5G1\nSP\nDE59FG1\nSP\nDE59AG1\nSP\nDE59CG1\nSP\nDE5A2G1\nSP\nDE5A4G1\n\nS\n" | $exec -c -n -o$outfolder'PeBE' >/dev/null 2>&1

This script uses an executable (dyngraph.exe for Windows or dyngraph for Linux) to process a trajectory file (obs.trj) and outputs the results to a specified folder.

Conclusion

Combining Git Bash and PowerShell can enhance your scripting capabilities on Windows. You get the powerful Unix-style commands from Git Bash alongside the Windows-specific management capabilities of PowerShell. Whether you’re a developer looking to streamline your workflows or an admin managing multiple systems, this hybrid approach can save time and add flexibility to your scripts.

Petros Aristidou
Petros Aristidou
Assistant Professor