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

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:
//stackoverflow.com/questions/4774054/reliable-way-for-a-bash-script-to-get-the-full-path-to-itself
# Absolute path to this script. /home/user/bin/foo.sh
SCRIPT=$(readlink -f $0)
# Absolute path this script is in. /home/user/bin
SCRIPTPATH=$(dirname $SCRIPT)

Reader's Comments

  1.    

    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 to Ed Allen Cancel reply

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

%d bloggers like this: