Fritz Thomas

06 Feb, 2009

How to get the absolute path within the running bash script

Posted by: Fritz Thomas In: Linux

Take a look at my updated post

Have you ever needed the absolute path of the shell script within this script? I do not mean the current working dir (pwd). That is different, because you do not know from which path the bash script is called.

In my case i needed the absolute path, because i wanted to call another script, which is by convention, in the same directory as the called script. In such a case you would use realpath in PHP – readlink is the bash equivalent to it.

1
2
3
4
# 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`
  • Digg
  • del.icio.us
  • DZone
  • MisterWong
  • Reddit
  • Slashdot
  • Technorati
  • Google Bookmarks
  • Facebook
  • Twitter
  • Yahoo! Bookmarks
  • Yigg
  • LinkedIn
  • MySpace
  • PDF
  • RSS

10 Responses to "How to get the absolute path within the running bash script"

1 | jose galvez

February 17th, 2009 at 10:26 pm

Avatar

a simple python script will get this for you. put this file in your path

file abspath
#!/usr/bin/env python

import os
import sys

print os.path.abspath(sys.argv[1])

then from your shell script you can
echo `abspath $0`

that will get you the absolute path to the script

if you want just the dirname part change sys.argv[1] to os.path.dirname(sys.argv[1])

3 | PongaPundit

July 31st, 2009 at 3:27 pm

Avatar

$0 within the script gives the script file’s filespec with relative path which should be sufficient to access other files located in the same folder as of the script’s.

4 | PongaPundit

July 31st, 2009 at 3:37 pm

Avatar

Example of using $0 to fetch the relative path:

$ pwd
/home/a/b/c

$ cat mytest.sh

echo Script Path is: `dirname $0`

$ cd /home/x/y/z
$ sh /home/a/b/c/mytest.sh
Script Path is: /home/a/b/c

5 | mina86

December 5th, 2009 at 9:10 pm

Avatar

Note that it’s not only inaccurate, it will also fail if script is in $PATH. See question 1.14 at .

6 | DarthMaul

December 22nd, 2009 at 10:33 pm

Avatar

Thank you. You seem to be the only one who could answer this for me. Everyone else seems to recommend using $PWD or pwd command and just won’t listen to the fact that you won’t necessarily be in the same directories as the files you want the absolute paths of.

7 | Aleksandr

January 7th, 2010 at 4:47 pm

Avatar

Hi! Thank you, it really works, you could even do it in one line like here:
http://www.devhands.com/2010/01/how-to-get-the-full-path-from-the-running-bash-script/

8 | Aaron

April 23rd, 2010 at 3:42 pm

Avatar

Neat! This method does require GNU readlink, rather than BSD readlink, which is a problem for Mac users and BSD hippies.

10 | How to get absolute path within shell script – PART2 | Linux | Fritz Thomas

June 28th, 2010 at 2:22 pm

Avatar

[...] is an update and in depth look to my previous Blog post of “How to get absolute path within the running shell [...]

Comment Form



Latest Tweets