How do I get the path of app in its launch script? I made an app by following these steps


Make folder Foo.app
Make Subfolder Contents
Put Info.Plist in Contents with

...
&lt;key&gt;CFBundleExecutable&lt;/key&gt;
&lt;string&gt;Foo&lt;/string&gt;
...

Make MacOS/Foo as an executable shell script


Foo looks like

#!/bin/sh
echo $0 &gt; /Users/me/temp/foo1
pwd &gt; /Users/me/temp/foo2
export &gt; /Users/me/temp/foo3


Clicking on the app in the finder then checking the results. The results are, foo1 is empty, foo2 is just / and foo3 looks like

export Apple_PubSub_Socket_Render="/private/tmp/com.apple.launchd.FgQwJ5EVCp/Render"
export HOME="/Users/me"
export LOGNAME="me"
export OLDPWD
export PATH="/usr/bin:/bin:/usr/sbin:/sbin"
export PWD="/"
export SHELL="/bin/zsh"
export SHLVL="1"
export SSH_AUTH_SOCK="/private/tmp/com.apple.launchd.4hbrl5p8fp/Listeners"
export TMPDIR="/var/folders/7h/wyw4jhd933z5jmqzsdx6r0sr0000gn/T/"
export USER="me"
export __CF_USER_TEXT_ENCODING="0x1F5:0x0:0x0"


No references to /Applications/Foo.app anywhere

Now do I get the path to my app in the launch script?
You can use this code:

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &amp;&amp; pwd )"
echo $DIR &gt;  /Users/me/temp/outLog.txt


It'll give you path to the MacOS folder of you app.

Here's great stackoverflow answer about this.