1

In a bash script, is there any method to get the full path of itself?

$ source ~/dev/setup.sh

I have tried $_, $-, $0..., but all are irrelevant.

Thanks in advance.

tthsn
  • 263
  • 2
  • 9
  • 2
    Possible duplicate of [Reliable way for a bash script to get the full path to itself?](http://stackoverflow.com/questions/4774054/reliable-way-for-a-bash-script-to-get-the-full-path-to-itself) – Biffen Mar 01 '16 at 09:27

3 Answers3

2

The $_ variable for location of a sourced script.

If you're sourcing the script, you can access the script name with $_ (since $0 will be the bash interpreter)

Pure bash solution.

relname="$_";

# if script is in current directory, add ./
if [[ ! "$relname" =~ "/" ]]; then 
   relname="./$relname"; fi

relpath=`dirname $relname`;
name="${relname##*/}"

# convert to absolute path by cd-ing and using pwd.
cd "$relpath" >/dev/null;
path=`pwd`;
cd - >/dev/null;

# construct full path from absolute path and filename
fullpath="$path/$name";

Note that this script has the side effect of replacing the $OLDPWD (used by cd -) by the script directory. If you need to avoid this, just save $OLDPWD before the cd - and restore it at the end.

Using realpath.

Assuming you have the realpath command installed, you can replace this code by the simple:

fullpath=`realpath "$_"`
pyrocrasty
  • 717
  • 8
  • 11
1

You can use BASH_SOURCE variable and then use readlink to get full path:

echo $BASH_SOURCE

# to get full path
fullpath=$(readlink --canonicalize --no-newline $BASH_SOURCE)
echo "$fullpath"

This will print the invoke path of the file being sourced, so in your case:

~/dev/setup.sh
~/dev/setup.sh

Reference

BASH_SOURCE

An array variable whose members are the source filenames where the corresponding shell function names in the FUNCNAME array variable are defined. The shell function ${FUNCNAME[$i]} is defined in the file ${BASH_SOURCE[$i]} and called from ${BASH_SOURCE[$i+1]}

Community
  • 1
  • 1
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • This doesn't provide the full path. It provides whatever relative path you run the script from. – 123 Mar 01 '16 at 09:32
  • That's what I mentioned in my answer that **This will print the invoke path of the file being sourced** – anubhava Mar 01 '16 at 09:34
  • 1
    Yes, that's the answer. Thank you very much. – tthsn Mar 01 '16 at 09:34
  • I've also added a suggestion of calling `readlink` in case this file is being sourced with relative path. – anubhava Mar 01 '16 at 09:36
  • @anubhava None of that was there when i commented. – 123 Mar 01 '16 at 09:47
  • You can check the edit history and see that `invoke path` line was always there esp since OP was invoking it as a full path script. However I added `readlink` to make it work in case relative path is being used also. – anubhava Mar 01 '16 at 09:49
  • You can edit with 5 minutes and it doesn't show up in the edit history. – 123 Mar 01 '16 at 09:53
  • 1
    You may be aware that 5 min limit doesn't apply if a single comment appears and then it shows up in edit history – anubhava Mar 01 '16 at 09:57
0

Use realpath.

$ realpath ~/dev/setup.sh

In a script:

rp=$(realpath "$0")
echo $rp
  • Note that one might want to give `realpath` some options, depending on how one would want it to deal with symlinks. – Biffen Mar 01 '16 at 09:33