When writing a bash script, how do I get the absolute path of the location of the current file?
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|
replaceold
withnew
-
\(.*\)/
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
Related videos on Youtube
dimpol
Updated on September 18, 2022Comments
-
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 ofmyBash.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 about 7 yearsThis might be better suited for stackoverflow. Check these questions: stackoverflow.com/questions/4774054/… stackoverflow.com/questions/59895/….
-
Sergiy Kolodyazhnyy about 7 yearsuse how.? please clarify
-
Zanna about 7 years@SethosII this question is totally on topic here
-
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 about 7 yearsI 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 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 about 7 yearsThanks 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 about 7 yearsQuestions 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 about 7 yearsPlease note that sometimes there is no path. For example:
cat /foo/bar | bash
.
-
-
Sergiy Kolodyazhnyy about 7 years
which
is more suitable for when script resides in one of the directories that are part ofPATH
variable. Use$0
from within a script, just like Zanna shows. But your answer is proper since you usedirname
instead of messing withsed
, hence +1 for that -
Sergiy Kolodyazhnyy about 7 years+1 for use of
$0
andsed
magic. But the saidsed
magic is really unnecessary when tools likedirname
exist -
Sergiy Kolodyazhnyy about 7 yearsyou already have the answer , use
realpath
:realpath "$( dirname $0 )"
I personally would usereadlink
but that's me:readlink -e $(dirname $0)
-
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 about 7 years@Serg
"$(dirname "$(realpath "$0")")"
is the most straightforward way. Quotes inside$()
work as normal. -
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 about 7 years@DepressedDaniel has stated the actual most straightforward solution.
-
ozw1z5rd almost 3 yearsIt's the best solution I found. Simple, easy, clear, working.
-
nilinswap over 2 yearsBeautiful solution