I'm using msysgit on Windows and wrote a simple shell script that I would like to use. I added the folder that the .sh file is in to the PATH variable of my Windows computer but when I want to use my script I have to type rd.sh instead of just plain rd.
How do I use this by just referring to its name rd and not the full file name with the .sh file extension (rd.sh)?
The shell provided with msysgit is bash, which runs in an emulated Unix-like environment.
If you're running the script from the bash shell, you need to type the name of the script, which as far as the Unix-like environment is concerned is rd.sh
. The .sh
has no particular meaning in a Unix environment; it's just the last three characters of the file name. The first line of the script should be #!/bin/sh
or #!/bin/bash
; this is known as a "shebang".
On the other hand, if you want to run it from Windows (say, from a cmd.exe
command prompt), then the .sh
extension is used by Windows to determine how to execute it, and you can invoke it as rd
if (a) it's in a directory in your %PATH%
, and (b) Windows is configured (in Folder Options and/or by setting %PATHEXT%
) to use sh
or bash
to launch .sh
files.
If you want to be able to run the same script from either environment, you can create a symbolic link that will be recognized in the emulated Unix-like environment. For example, if rd.sh
is in $HOME/bin
, then this:
ln -s $HOME/bin/rd.sh $HOME/bin/rd
will create the appropriate symlink. (You could make rd
a copy of rd.sh
, but then changes to one won't apply to the other.)
If you only need to run it from bash, just call it rd
rather than rd.sh
; as I mentioned, as far as bash is concerned the .sh
extension is just part of the name and has no particular significance. It's the #!
line, not the .sh
extension, that tells bash how to execute the script.
(Well, strictly speaking it's not bash that handles the #!
. On actual Unix or Linux systems, it's handled by the kernel; I'm not sure what the exact mechanism is under msysgit.)