Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Allow .command files to determine working directory UNIX
An earlier hint describes how you can use the .command filename suffix on a shell script to make it double-clickable in the Finder. This is very handy, but it turns out that such scripts start up with their current working directory set to the home directory of the current user. This makes it inconvenient to distribute .command files that come with additional resources that they expect to find via paths relative to the directory in which the .command file is located.

In order to set the current working directory of the running script to the folder in which the .command file is located, simply add this line at the top of the shell script:
cd `dirname $0`
Here's how this works: $0 is the reserved variable that returns the current command being executed; in our case, $0 contains the absolute path of the .command file itself. The dirname utility then takes that path and returns the absolute path of the directory containing the .command file. Putting the dirname command in backquotes (`, not to be confused with the normal single quote character ') executes dirname and substitutes its output into the current command line, allowing it to become the argument to the cd command. So in English, what we get is essentially: "Change to the directory containing this file."

Script code executed after this can use relative pathnames that will work regardless of where the .command file is located, with one caveat that I know of: the path to the .command file cannot contain spaces, single-quotes, or other characters that have special meaning to the shell. If anyone knows of a nice, elegant workaround for this, I'd be interested to hear it.
    •    
  • Currently 3.57 / 5
  You rated: 3 / 5 (7 votes cast)
 
[34,250 views]  

Allow .command files to determine working directory | 7 comments | Create New Account
Click here to return to the 'Allow .command files to determine working directory' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Allow .command files to determine working directory
Authored by: merlyn on Dec 20, '04 09:17:44AM
To handle pathnames with spaces and other shell-significant characters, I use this code:

#!/bin/sh
here="`dirname \"$0\"`"
echo "cd-ing to $here"
cd "$here" || exit 1
As best I can tell, this protects all odd characters at each stage. Just another guy who has been playing with the Unix shell since 1977 (yes, *77*),

[ Reply to This | # ]
Allow .command files to determine working directory
Authored by: owsla on Dec 20, '04 09:27:33AM

This also seems to work (bash 2.05b):

cd "`dirname "$0"`"

Minimal testing indicates it handles spaces just fine.



[ Reply to This | # ]
Allow .command files to determine working directory
Authored by: pediddle on Dec 20, '04 02:52:03PM

But your version doesn't do error checking. I guess the only question is whether the \" escaped quotes are necessary.



[ Reply to This | # ]
Allow .command files to determine working directory
Authored by: davemak on Dec 20, '04 05:35:45PM

Yes, this is what I'm using now. I actually figured it out about 30 minutes after posting the hint, but couldn't get in to edit the hint until after it actually got posted to the site.



[ Reply to This | # ]
Allow .command files to determine working directory
Authored by: miggins on Dec 20, '04 08:08:04PM

Just be aware that this only works when the BSD subsystem is installed. (lsbom /Library/Receipts/BSD.pkg/Contents/Archive.bom | grep dirname).



[ Reply to This | # ]
Allow .command files to determine working directory
Authored by: kholburn on Dec 21, '04 09:51:53AM
dirname is a program so running `dirname $0` runs another program (which should be there). There is a way to do it using just bash.

#!/bin/sh
dir=${0%/*}
if [ "$dir" = "$0" ]; then
  dir="."
fi
cd "$dir"


or

#!/bin/sh
dir=${0%/*}
if [ -d "$dir" ]; then
  cd "$dir"
fi


[ Reply to This | # ]
Allow .command files to determine working directory
Authored by: las_vegas on Dec 26, '04 03:19:36PM
A simpler and easier to read method of entering this command is:

cd $(dirname "$0")

This simply uses a different technique of executing a command within the script. I find the left quote character difficult to see and understand what the script is doing.

[ Reply to This | # ]