7

In Mac OS X, from a shell script application's point of view, how can I get its bundle directory path for use in a shell script? When using the env command, it returns that the app's execution path is /, and no environment variable helps. I'm doing this because I would like to access some resources of the bundle, and because the app is not always installed in /Applications.

Thanks!

3
  • 1
    Does the shell script belong to the app resources? Anyway, this question belongs to Stackoverflow.
    – mouviciel
    Jun 24, 2010 at 11:20
  • In my case, the shell script IS the App's executable.
    – moala
    Jun 24, 2010 at 13:09
  • I've always wanted to know if/how I could do this.
    – Jeremy
    Aug 14, 2010 at 4:49

4 Answers 4

6

DIR=$(cd "$(dirname "$0")"; pwd) will give the shell script's directory name. For a shell script app, this will be /path/to/Your shell script application.app/Contents/MacOS.

2

I was suggesting to try which, but it doesn't work for Mac applications. You can use find, keeping in mind that Mac applications are bundled, and a shell sees them as directory. Don't forget to add the extension for applications (.app).

2
  • The problem with find is that it is VERY long, and may find another copy of the program... The problem with which is that it does not find any .app bundle...
    – moala
    Jul 7, 2010 at 13:35
  • Even when the answer is old as jeruzalem, it helped me... Totally forgot about which, which is already working on mac (zsh) Oct 13, 2022 at 19:22
1

If you can get the application's absolute path to the executable, getting the bundle dir is as simple as appending that with /../.. that gets you the main bundle. If you want the resources however, just add to the above /Resources.

0

Edit: You want something completely different than my original answer. Keeping it below in case it is useful to anyone. Now I think you are looking for the basename command, although I'm still not certain and I think you'll need to specify more information about what you are doing before we'll be able to give you a decent answer.

I'm not sure I understand the question. Are you trying to locate the application directory? E.g. for Calculator, you want the directory that Calculator.app is in?

jed@jed-osx:~$ ls -la /Applications/Calculator.app/
total 0
drwxr-xr-x   3 root  wheel   102 Jun 22 11:27 .
drwxrwxr-x+ 46 root  admin  1564 Jun 18 23:19 ..
drwxr-xr-x  10 root  wheel   340 Jun 22 11:26 Contents

jed@jed-osx:~$ ls -la /Applications/Calculator.app/Contents/
total 8
drwxr-xr-x  10 root  wheel   340 Jun 22 11:26 .
drwxr-xr-x   3 root  wheel   102 Jun 22 11:27 ..
lrwxr-xr-x   1 root  wheel    28 Apr 17 15:15 CodeResources -> _CodeSignature/CodeResources
-rw-r--r--   1 root  wheel  1201 May  6 10:26 Info.plist
drwxr-xr-x   3 root  wheel   102 Jun 22 11:26 MacOS
-rw-r--r--   1 root  wheel     8 Jul  6  2009 PkgInfo
drwxr-xr-x   4 root  wheel   136 Jul  6  2009 PlugIns
drwxr-xr-x  42 root  wheel  1428 Apr 17 15:28 Resources
drwxr-xr-x   3 root  wheel   102 Jun 22 11:26 _CodeSignature
-rw-r--r--   1 root  wheel   451 May  6 10:27 version.plist

Or are you looking for something completely different?

3
  • Completely different. Sorry for not being clear enough. It's from the app's executable point of view that I would like to get the path of the bundle...
    – moala
    Jun 24, 2010 at 9:30
  • 1
    AH. Ala basename in a bash script? It sounds like this question would be better placed over on Stack Overflow (it has probably already been asked and answered). Jun 24, 2010 at 9:34
  • Mmmh not working with basename... I found a different solution: DIR=$(cd "$(dirname "$0")"; pwd) is it better or equivalent?
    – moala
    Jun 24, 2010 at 13:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .