#terminal

In case you need to get the absolute path to the folder in which a bash script exists (e.g. when you need to refer to a relative binary), you can do it like this:

myscript.sh

1#!/usr/bin/env bash
2
3SCRIPTPATH="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
4echo $SCRIPTPATH

So, if you run the script, you will get:

1$ ./subfolder/myscript.sh
2/Users/user/bin/subfolder

You can then use it to easily refer to a relative binary or file in a reliable way. We use this to create a script which uses xcodebuild but applies xcbeautify to it's output. As we are using a custom build of xcbeautify and have it in the same folder as the script, this trick becomes really handy:

xcodebuild.sh

1#!/usr/bin/env bash
2
3SCRIPTPATH="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
4PRETTIER=$SCRIPTPATH/xcbeautify
5
6set -o pipefail && xcodebuild "$@" | $PRETTIER --disable-colored-output
7
8exit $?