Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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!

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

4 Answers 4

up vote 2 down vote accepted

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.

share|improve this answer
add comment

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).

share|improve this answer
    
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 '10 at 13:35
add comment

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.

share|improve this answer
add comment

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?

share|improve this answer
    
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 '10 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). –  Jed Daniels Jun 24 '10 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 '10 at 13:07
add comment

Your Answer

 
discard

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.