Git Bash on Windows - Where to Store and How to Invoke Bash Scripts
Asked Answered
M

3

10

Using Git Bash on Windows, if I create a bash script, where do I put it so that I can access it from the bash shell?

Basically, I want to create a bash shell script that does some stuff with files etc.

I want to invoke the function from inside the bash shell with some parameters. The script will then do its work.

I am completely new to this environment. So, my knowledge of bash is very limited.

Merrillmerrily answered 20/8, 2010 at 17:14 Comment(0)
G
5

Well, you can put it anywhere you want. But for ease of use, you might want to put it in your home directory. To find it, open up Git Bash and type the following:

cd ~
pwd

It should return something like /c/Documents and Settings/username (or /c/Users/username). In Windows terminology, that it at "C:\Documents and Settings\username" as you would expect.

Gogh answered 20/8, 2010 at 18:11 Comment(2)
So does it need to be named something in particular, or it executed similarly to a BAT file ?Merrillmerrily
Naming shouldn't matter. However, usually the first line of the file points to the shell that executes it, so in this case the first line should be #!/bin/bash. Also, there's a chance you may need to mark the file as executable with the command chmod a+x scriptname, but it's not likely to be necessary here.Gogh
H
0

There are several places you can put your scripts from which they are auto-loaded. You can execute this command to see which location are being loaded on your system, and in which order:

echo $PATH

Place a script in one of the locations (or create it if it doesn't exist yet) and it will be executable. You can also modify the $PATH variable in your .bashrc to add a custom location.

It's important to note though that some of the paths, like /bin or /usr/bin usually contain system-critical programs and when some script is broken in there you might not be able to start at all. Paths like /c/Users/username for your user specifically, or /usr/local/bin for all users, are usually preferred.

Homework answered 29/12, 2017 at 11:22 Comment(0)
C
-4

You can put it in /bin which is equivalent to C:\Program Files\Git\bin (or similar, depending on where you installed git).

Cummins answered 20/8, 2010 at 17:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.