MacOS – Command in bash shell script to find path to that script

bashcommand linemacospathscript

Is there a command to go inside a bash .sh script that will provide the full path to the directory containing that script?

Best Answer

See the answers on Get the source directory of a Bash script from within the script itself. The accepted one recommends

#!/bin/bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

but reading all the answers gives a lot of alternatives (and insights into how shells work).