I want to have a bash-script discover its own path.

The background is that I have a script called process_scanned_text.sh, which itself uses the script curves. I put curves in the same directory as process_scanned_text.sh. I created a symlink to process_scanned_text.sh in my ~/bin folder. Now I don't know how to correctly call the curves script. As it is not in $PATH, I need to specify it's path. Using dot as path is not correct as this points to the directory from which I call process_scanned_text.sh. Using dirname $0 does not work either as this points to my bin folder (where the symlink is).

  • 1
    Try dirname $(readlink -f $0) – Mark Plotnick Dec 22 '14 at 19:23
  • @MarkPlotnick, that's the answer, you should add an answer – glenn jackman Dec 22 '14 at 19:31

On Linux systems that have GNU coreutils installed, or on FreeBSD >= 8.3:

In a shell script, call readlink -f "$0" to find the canonical pathname of the script, which will resolve any symlinks. Call dirname on that to get its directory name.

  • The -f option does not seem to be portable. – Celada Dec 22 '14 at 19:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.