14

I want to create a simple bash script to launch a Java program on OS X. The names of the file, the file path, and the immediate working folder all contain spaces. When I do this:

#!/bin/sh cd `dirname $0` 

I get

usage: dirname path 

I have also tried putting quotes in all kinds of different places. The most elaborate example being

cd "`dirname \"$0\"`" 

Nothing has worked. I either get error messages or that cryptic "usage: dirname path" message.

What are other methods that might work?


Edit: this doesn't seem to be an issue for anyone but me so it must just be my box. I'm going to accept my own post below because it's the only solution which worked for this specific problem. However I'm definitely upvoting the solutions which seem to be working for everyone else and really appreciate everyone's help.

4
  • What does echo $0 yield? dirname might have a problem with something specific about the value of $0. Have you tried running this with the script in a different directory? Commented Aug 20, 2009 at 21:15
  • Your second example works for me with a directory six levels deep where every other ancestor has at least one space in its name. Commented Aug 20, 2009 at 21:20
  • ever try looking at dirname's manpage? Commented Aug 20, 2009 at 21:21
  • @Matt - I'm looking at it right now. What am I looking for? Commented Aug 20, 2009 at 21:23

5 Answers 5

14

What about:

cd "$(dirname "$0")" 

That works for me here.

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

Comments

6
#!/bin/sh cd "$(dirname "$0")" 

Comments

6

What finally worked for me is changing this:

#!/bin/sh cd `dirname $0` 

To this:

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

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

4 Comments

Just curious - what was failing with my and Ned's solution? I'd like to update my answer if appropriate for the archives.
@Sean: I still kept getting "usage: dirname path". I think it's something to do with my machine. But this z shell solution worked for me. Weird.
you might also try /bin/bash - I think /bin/sh is a symlink to /bin/bash, but bash looks at how it was invoked, and changes some things in it's behaviour accordingly. So if you invoke it with /bin/sh
@PeterBagnall I believe you are correct. He should be using /bin/bash... stackoverflow.com/questions/11061727/…
3

Escaping the inner double quotes is unnecessary:

cd "`dirname "$0"`" 

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

bash -x scriptname 

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

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

Comments

1

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

#!/bin/sh 

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

#!/usr/bin/bash 

Pretty new to the whole scripting thing so not sure.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.