Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git for windows bash execution path for a script not correct

When the code

SRC=$(cd $(dirname "$0"); pwd)

is executed inside a Bash script on a system that has Git for Windows installed in c:\program files\Git, the SRC will get the value /c/program.

How do I make this work? I need to get the path to where the script is and use that to include other scripts with a relative path. In my case the next line is: source "${SRC}/common/common.sh"

The script is in c:\program files\Git\usr\bin so I can use git <command-name> and extend Git with some useful hand-made commands for feature branch workflow with rebase and submodules.
The source code is at https://github.com/jlovs/git-scripts if anyone want to help out.

like image 745
Jan Lovstrand Avatar asked Oct 26 '25 14:10

Jan Lovstrand


1 Answers

You need to double-quote the argument of the cd command.

SRC=$(cd "$(dirname "$0")"; pwd)

The command substitution $(dirname "$0") expands to a path containing a space. The following cd command gets two arguments while you want to pass just one, with a space inside.

You don’t need to worry about the quotes around $0 inside another pair of quotes since the $() command substitution starts a new quoting context.

like image 86
Melebius Avatar answered Oct 29 '25 05:10

Melebius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!