4

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

HalosGhost
  • 4,732
  • 10
  • 33
  • 41
jan
  • 881
  • 2
  • 8
  • 18

1 Answers1

7

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.

Mark Plotnick
  • 24,913
  • 2
  • 59
  • 81