简体   繁体   中英

Run shell script globally in Git bash in Windows

I have a shell script that I want to run globally. It's current directory is

~/directory/script.sh

I can run the script only from the directory in which the script is located, however, I'd like to run it from every directory, not just the one that it's in.

I'm using Git bash in Windows 10. I've tried putting this line at the top of the file:

#!usr/local/bin

but that didn't work (probably because I'm using Windows), the bash said it doesn't recognize it.

I also tried using alias but it's not really a solution.

Is there a different way to run a shell script globally?

git bash loads its global scripts from a directory /usr/bin which it creates in its windows installation folder, if you place your script there, you should be able to call it from any directory you're in while using git bash

Find your root directory of Git Bash installation and go to /usr/bin .

For my case, full path is: C:\Program Files\Git\usr\bin

Copy your script there (you will need Administrator rights). I named my script as my-script.sh .

#!/bin/bash
echo "Hello World"

Open Git Bash as an Administrator and give your script executable right:

chmod +x /usr/bin/my-script.sh

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM