Updated : Aug 15, 2015 in Bash

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/foo.sh
SCRIPT=$(readlink -f $0)
# Absolute path this script is in. /home/user/bin

1 Comment

  • 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.

Leave a Reply

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

%d bloggers like this: