Fritz Thomas

06 Feb, 2009

How to get the absolute path within the running bash script

Posted by: Fritz Thomas In: Linux

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
  • Fark
  • Google Bookmarks
  • Facebook
  • MisterWong.DE
  • Twitter
  • Twitthis
  • Yahoo! Bookmarks
  • Yigg

7 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/

Comment Form


  • subbu: Hey, Thanks a lot. I was searching for something similar. Subbu
  • Maik: Habe jetzt für ein eigenes (schon ewig geplantes) Projekt auch mit dem ZF(version 1.10) angefangen und das tutorial hier hat trotzdem noch ganz gut g
  • Andreas: Auf jeden Fall eine gelungene Einführung, danke. Vielleicht gibt es ja 2010 den zweiten Teil ;)
  • Hartmut: Genau das Leserecht war bei mir das Problem. Danke.
  • admin: Hi! For more information go to http://extjs.com/ . There is a preview video of this Tool called Ext.De
  • jagocoding: nice info bro... where can i download this tools ?? can u give me ulr, this is free or not ?
  • admin: Of course! This should be rm -rf /tmp/*
  • Christian: rm -rf /tmp* bad idea. at least do rm -rf /tmp/* but if things go worse it will skrew your X Session
  • Aleksandr: 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
  • matt: thanks for the tip. freeing up the distfiles freed me up 5GB of diskspace. My root is only 20GB which I thought should be plenty but my disk hit 100