Get the Absolute Path of an Executing Shell Script in Linux

It is often helpful to know the absolute path of an executing shell script at run time. Fortunately this is easy to do with the realpath utility widely available on most Linux distributions.

realpath takes the relative or absolute path of a file as an argument. $0 is a special bash variable that will expand to the name/relative path of the script being called. $0 can be passed to realpath.

Getting the directory containing of an executing shell script can also be found by using the realpath and dirname utilities together.


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">