MAC OS X, Linux, Windows and other IT Tips and Tricks

15 Aug 15 Finding the absolute path of a running script

In bash scripts we often need to know in which directory the running script is found especially when the script is in the $PATH and may be occurring in multiple places:
Here is a reliable way to find it out:
Based on this site:
# Absolute path to this script. /home/user/bin/
SCRIPT=$(readlink -f $0)
# Absolute path this script is in. /home/user/bin

Reader's Comments


    How about:

    readlink -f $(which — $0)

    ‘which searches PATH for basename match and returns the full path of the first match.

    readlink -f $(which — awk)

    Is a good example.

    Reply to this comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: