After a little googling, I found this solution by netizen apokalyptik:
#!/bin/bash LSOF=$(lsof -p $$ | grep -E "/"$(basename $0)"$") MY_PATH=$(echo $LSOF | sed -r s/'^([^\/]+)\/'/'\/'/1 2>/dev/null) MY_PID=$$ MY_ROOT=$(dirname $MY_PATH) MY_NAME=$(basename $0) echo -e "PATH\t$MY_PATH" echo -e "FILE\t$MY_NAME" echo -e "CWD \t$MY_ROOT" echo -e "PID \t$MY_PID"The trick lies in the use of the lsof command, which returns a list of currently open files. From there, the script uses grep to locate the entry corresponding to the currently running script, sed to separate the full path from the entry, then basename & dirname to split the base directory and file name.
This solution could be further enhanced by adding the current user to the
grepregular expression: as it is, it could return the path to a same-named file opened by another user – but unless you work on a multi-user environment, it shouldn't make much difference.