Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm setting up a project template for my office that uses .command files to encapsulate some command-line functions, since not everyone in the office is terminal-savvy. Mostly this is going to involve some path-changing within the project, and launching some compilers/watchers like compass or grunt.

Here's the problem I'm running into - with each project having it's own svn repo, the path to any given project root is going to be different for each user:

  • Bob: /Users/bobsmith/work/clients/client-a/project-a
  • Jane: /Users/janedoe/web/projects/project-a

I want to put .command files inside a project that are capable of finding the project root, regardless of where the project is located within the system. Is there any way to accomplish this? Maybe by referencing the path of the .command being executed? Here's a sample .command that's already in place:

# Replace this path with your project directory - remember to ignore it in svn!
cd /Users/janedoe/web/projects/project-a
grunt watch
share|improve this question
If there's only one project per user, you can set an environment variable in the user's plist which points to the project, otherwise you can use a wish prompt every time the program loads, asking which project the user wants to mess with. – Parthian Shot Jun 16 '14 at 20:02
Oh, wait... the .command is already in the project... Then you can just use cd "$(dirname "$0")", which will cd into the directory in which the command file is kept. Or, at least, that's the bash way to do it. – Parthian Shot Jun 16 '14 at 20:05
up vote 2 down vote accepted
# This will cd into the directory in which the .command file exists
cd "$(dirname "$0")"

On the other hand...

#  This will define an environment variable that contains the full absolute path 
#+ to the directory in which the .command file is kept, and then will cd into 
#+ that directory
ABSPATH="$(cd "$(dirname "$0")" && pwd)"

Why all the double quotes?

Without them, things break when the user has spaces in the path to the project. Also- probably obvious- but this will also break if the user can't cd into the directory of their project.

Why $( ... ) rather than ` ... ` ?

That first syntax is newer, and preferred over the second one. Besides which, it's easier to see. If you think matching parens is annoying, try matching backticks- especially when there are normal single quotes interspersed between them. Both of these do the same thing; fork a subshell and run their internal command within that subshell.

share|improve this answer
Works like a charm - thanks! – CodeMoose Jun 16 '14 at 20:23

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.