We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
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
#!/usr/bin/env bash
SCRIPTPATH="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
echo $SCRIPTPATH
So, if you run the script, you will get:
$ ./subfolder/myscript.sh
/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
#!/usr/bin/env bash
SCRIPTPATH="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
PRETTIER=$SCRIPTPATH/xcbeautify
set -o pipefail && xcodebuild "$@" | $PRETTIER --disable-colored-output
exit $?
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.