简体   繁体   English

在 Git bash 在 ZAEA23489CE3AA9B6406EBB28E0CDA4 中全局运行 shell 脚本

[英]Run shell script globally in Git bash in Windows

I have a shell script that I want to run globally.我有一个想要在全球范围内运行的 shell 脚本。 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:我在 Windows 10 中使用 Git bash。我试过把这行放在文件的顶部:

#!usr/local/bin

but that didn't work (probably because I'm using Windows), the bash said it doesn't recognize it.但这不起作用(可能是因为我使用的是 Windows),bash 说它无法识别它。

I also tried using alias but it's not really a solution.我也尝试过使用alias ,但这并不是真正的解决方案。

Is there a different way to run a shell script globally?是否有其他方法可以在全球范围内运行 shell 脚本?

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 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 .找到 Git Bash 安装和 go 到/usr/bin的根目录。

For my case, full path is: C:\Program Files\Git\usr\bin就我而言,完整路径是: C:\Program Files\Git\usr\bin

Copy your script there (you will need Administrator rights).在那里复制您的脚本(您将需要管理员权限)。 I named my script as my-script.sh .我将我的脚本命名为my-script.sh

#!/bin/bash
echo "Hello World"

Open Git Bash as an Administrator and give your script executable right:以管理员身份打开 Git Bash 并赋予您的脚本可执行权限:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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