Command in bash shell script to find path to that script?
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]}" )" &gt;/dev/null 2&gt;&amp;1 &amp;&amp; pwd )"


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