It is often helpful to know the absolute path of an executing shell script at run time. Fortunately this is easy to do with the realpath
utility widely available on most Linux distributions.
realpath
takes the relative or absolute path of a file as an argument. $0
is a special bash variable that will expand to the name/relative path of the script being called. $0
can be passed to realpath
.
Getting the directory containing of an executing shell script can also be found by using the realpath
and dirname
utilities together.
Example:
#!/bin/bash
echo "printing path of executing script"
realpath $0
echo "printing directory of executing script"
dirname `realpath $0`