[ACCEPTED]-OS X bash: dirname-dirname

Accepted answer
Score: 14

What about:

cd "$(dirname "$0")"

That works for me here.


Score: 6
cd "$(dirname "$0")"


Score: 5

What finally worked for me is changing this:

cd `dirname $0`

To 3 this:

#! /bin/zsh
cd "${0:h}"

This also supports file names and file 2 paths containing spaces. Here's where I 1 found it: http://rentzsch.com/unix/locationAwareCommandFiles

Score: 3

Escaping the inner double quotes is unnecessary:

cd "`dirname "$0"`"

But 7 that doesn't get to the root of the problem, which 6 is that somehow the value of $0 appears 5 to be empty, or perhaps something strange. Try 4 changing running your script this way:

bash -x scriptname

This 3 will echo each line, with variables interpolated, before 2 running it. It is very useful for debugging. Also 1 quite helpful are:

-u: abort on attempt to use undefined variable
-e: abort on first error
Score: 1

Hey not sure about this... But is it possible 3 that your


Points to something that is not 2 bash? What I usually use is:


Pretty new 1 to the whole scripting thing so not sure.

More Related questions