4

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.

6
  • 1
    Please share more information about the environment: Are you running under Cygwin? Commented Dec 13, 2017 at 7:34
  • I'm running MINGW64 Commented Dec 13, 2017 at 8:44
  • @JanLovstrand – there may be some ambiguity in exactly what you are asking. After reading it a second time, I honestly have no clue. What do you want SRC to be exactly? Commented Dec 13, 2017 at 8:49
  • Wait, I think I get it. You need to resolve the src directory: how about SRC=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) Commented Dec 13, 2017 at 8:54
  • @JanLovstrand – if this is incorrect please clarify the question. Commented Dec 13, 2017 at 8:56

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

This answer is perfectly valid, but for the double quote lovers out there (which I assume all Windows users need to be): SRC="$(cd "$(dirname "$0")"; "pwd")"
This one works. Sorry if I didn't notice that it was an included script that had the same error. Thank you all!
Shouldn't you escape internal quotes? SRC=$(cd "$(dirname \"$0\")"; pwd)
@phd No, see the last paragraph of my updated answer.
0

As you stated, you are running under MINGW64. You can try to use cygpath -aw to convert the path to windows format:

SRC=$(cygpath -aw $(cd "$(dirname "$0")"; pwd))

More information here and here.

3 Comments

Think the problem is in the $(dirname "$0") part of the line, because this gives me a "too many arguments" error. I think that the space in the path has to be escaped like /c/Program\ Files/Git/usr/bin, but don't know how to make that happen. Tried cygpath -u, but it gives me /c/Program Files/usr/bin
Try cygpath -aw
Please verify spaces, quotes, etc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.