9

I'm a longtime Windows and Linux user and have had some weird experiences writing shell scripts on Mac OS X. In particular, I can write the scripts just fine and run them from the terminal, but every time I try running one from a Finder window it always executes from the user's home directory rather than the directory in which the script lives. That is, if the script is located at

~/path/to/the/script.sh

it always runs out of ~. In the past I've had to ask my more Mac-savvy friends how to fix this, and they've often done so using some perversely awful techniques, but I don't see any reason why this should be the case.

My question is - is there an easy way to write a shell script in Mac OS X that, when double-clicked in Finder, runs out of the directory in which it resides?

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • 1
    Why do you expect it to do otherwise? This is just the defined behavior. – bmargulies Jan 05 '11 at 02:06
  • "root directory" != "user's home directory" – David Gelhar Jan 05 '11 at 02:08
  • At least in Windows the default behavior you get when running a program from an explorer window is that program running out of the directory from which it resides. Maybe this is the exception rather than the rule, but it's what I'm most used to. – templatetypedef Jan 05 '11 at 02:12
  • @OP That's not very intuitive default behaviour imo. That's largely to solve some issues inherent in the way win32 handles shared libraries, afaik. – richo Jan 05 '11 at 02:59
  • Not exactly relevant, but: isn't this the same behavior as Linux? I'm not sure offhand but I'm pretty sure double-clicking a shell script in, e.g., Gnome will execute it from the home directory, also. – Ken Jan 06 '11 at 07:35

4 Answers4

13

The behaviour seems consistent to me. It inherits your finder instances environment, which would be rooted at your home directory.

However, it's not too hard to fix.

cd "$(dirname "$0")"

Ought to do the trick ala:

richh-desktop % fullname.sh 
/home/richh/bin
richh-desktop % cd .. 
richh-desktop % fullname.sh 
/home/richh/bin
richh-desktop %        
richo
  • 8,717
  • 3
  • 29
  • 47
  • Please double-quote paths in case there are spaces in any of the filenames (this is OS X -- filenames have spaces in them): `cd "$(dirname "$0")"` – Gordon Davisson Jan 05 '11 at 16:49
2

If you want the script to run out of the directory it lives in, the script will have to change its working directory. Given that your example is written in bash, I'll respond with a bash example. The $_ value is set to the absolute path of the current script. You can find more information by running man bash

#!/bin/bash

PROGPATH=$(dirname $_)
cd $PROGPATH

# Do your stuff here
Zac Sprackett
  • 906
  • 6
  • 3
1

It's going to be up to the script to change the directory for you. Check this page out.

Community
  • 1
  • 1
Staros
  • 3,232
  • 6
  • 30
  • 41
1

The purpose of $0 is to solve this problem.

bmargulies
  • 97,814
  • 39
  • 186
  • 310