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


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 | # ]