When writing a bash script, how do I get the absolute path of the location of the current file?

64,199

Solution 1

You can get the full path like:

realpath "$0"

And as pointed out by Serg you can use dirname to strip the filename like this

dirname "$(realpath $0)"

or even better to prevent awkward quoting and word-splitting with difficult filenames:

temp=$( realpath "$0"  ) && dirname "$temp"

Much better than my earlier idea which was to parse it (I knew there would be a better way!)

realpath "$0" | sed 's|\(.*\)/.*|\1|'

Notes

  • realpath returns the actual path of a file
  • $0 is this file (the script)
  • s|old|new| replace old with new
  • \(.*\)/ save any characters before / for later
  • \1 the saved part

Solution 2

The accepted answer seems perfect. Here's another way to do it:

cd "$(dirname "$0")"
/bin/pwd

/bin/pwd prints the real path of the directory, as opposed to the pwd builtin command.

Solution 3

if the script is in your path you can use something like

$ myloc=$(dirname "$(which foo.sh)")
$ echo "$myloc"
/path/to/foo.sh

EDIT: after reading comments from Serg, this might be a generic solution which works whether the script is in your path or not.

myloc==$(dirname "$(realpath $0)")
dirname "$myloc"

Solution 4

wdir="$PWD"; [ "$PWD" = "/" ] && wdir=""
case "$0" in
  /*) scriptdir="${0}";;
  *) scriptdir="$wdir/${0#./}";;
esac
scriptdir="${scriptdir%/*}"
echo "$scriptdir"

It is taken as reference from kenorb and andro
No dirname, readlink, realpath, BASH_SOURCE
All are builtins

Share:
64,199

Related videos on Youtube

dimpol
Author by

dimpol

Updated on September 18, 2022

Comments

  • dimpol
    dimpol over 1 year

    Suppose I have a bash file called myBash.bash. It resides in:

    /myDirect/myFolder/myBash.bash
    

    Now I want to use the string /myDirect/myFolder (the location of myBash.bash) inside the script. Is there a command I can use to find this location?

    Edit: The idea is that I want to set-up a zip-folder with code that can be started by a bash script inside that zip-file. I know the relative file-paths of the code inside that zip-file, but not the absolute paths, and I need those. One way would be to hard-code in the path, or require the path of the file to be given as a variable. However I would find it easier if it was possible for the bash-file to figure out where it is on its own and then create the relevant paths to the other file from its knowledge of the structure of the zip-file.

    • Sethos II
      Sethos II about 7 years
      This might be better suited for stackoverflow. Check these questions: stackoverflow.com/questions/4774054/… stackoverflow.com/questions/59895/….
    • Sergiy Kolodyazhnyy
      Sergiy Kolodyazhnyy about 7 years
      use how.? please clarify
    • Zanna
      Zanna about 7 years
      @SethosII this question is totally on topic here
    • Sethos II
      Sethos II about 7 years
      @Zanna pwd returns your current directory (from where you run the scirpt) not the scripts location. And just for clarification: it's about bash scripting, which is part of ubuntu, but isn't it more about programming then the distribution itself?
    • dimpol
      dimpol about 7 years
      I added more context to the question. I thought there might be an easy/obvious command to get the file-path of the file that is executing, but it seems the answer is at least non-obvious.
    • Sethos II
      Sethos II about 7 years
      @Zanna I don't say it's offtopic, i think it's just more appropriate and as shown with the links already asked and answered there multiple times.
    • dimpol
      dimpol about 7 years
      Thanks for the help everyone! The answer is indeed to take the zeroth argument of the bash-file and then use functions like dirname and realpath to get where you want to be. My apologies that I wasn't able to find the previous questions on this topic
    • laurent
      laurent about 7 years
      Questions that have been answered 8 years ago on StackOverflow, have been viewed about a million times, and have over 4000 upvotes aren't very suited to post here. Well I guess it's not a duplicate here.
    • pipe
      pipe about 7 years
      Please note that sometimes there is no path. For example: cat /foo/bar | bash.
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy about 7 years
    which is more suitable for when script resides in one of the directories that are part of PATH variable. Use $0 from within a script, just like Zanna shows. But your answer is proper since you use dirname instead of messing with sed, hence +1 for that
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy about 7 years
    +1 for use of $0 and sed magic. But the said sed magic is really unnecessary when tools like dirname exist
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy about 7 years
    you already have the answer , use realpath: realpath "$( dirname $0 )" I personally would use readlink but that's me: readlink -e $(dirname $0)
  • David Foerster
    David Foerster about 7 years
    @Serg: Wouldn't dirname "$(realpath "$0")" be better? Usually one wants to know the parent directory of the referenced file instead of the parent directory of the symbolic link referring to said file.
  • DepressedDaniel
    DepressedDaniel about 7 years
    @Serg "$(dirname "$(realpath "$0")")" is the most straightforward way. Quotes inside $() work as normal.
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy about 7 years
    @DepressedDaniel Did a bit of research online, you're right: $() being a subshell will allow having quotes outside. I wouldn't say it's as straightforward though, unless one realizes $() are all subshells
  • Wildcard
    Wildcard about 7 years
    @DepressedDaniel has stated the actual most straightforward solution.
  • ozw1z5rd
    ozw1z5rd almost 3 years
    It's the best solution I found. Simple, easy, clear, working.
  • nilinswap
    nilinswap over 2 years
    Beautiful solution