In the modern days of CI/CD automation scripts, obtaining the absolute path of a shell script is the base for any useful script in a coding repository. Whether you want to automate filesystem operations, download content to a certain location, or write build scripts, you will need to get the location of your main directory in your scripts.
For this reason, we’ll look at different ways to get the absolute path of a script with a shell command. In addition, the methods we introduce in this post should be fairly portable between shell environments (bash
, zsh
, etc).
If you’ve just started out writing shell scripts, you may find it useful to read my post about the most useful terminal commands for beginners!
Video – How To Get The Path Of A Shell Script While Writing A Utility Script (Unzipping)
If you’re a visual learner, the video below shows you the best way to get a script path. Feel free to read the remaining sections for the same content in writen form!
Two Different Ways To Get The Path Of A Script: $0
Vs $BASH_SOURCE
Long story short, we rely on two different shell variables that contain (the relative) path of the script being executed.
Firstly, we will look at the variable $0
, which should contain the path of the script being executed. Unsurprisingly, this is probably the most popular option out there, as it’s very simple to use and it’s fairly well defined in non-bash shells.
Similarly, we also look at the ${BASH_SOURCE[i]}
variable (array), which in bash-derived shells, contains information about the definitions of call stack functions. More specifically. ${BASH_SOURCE[0]}
, or simply $BASH_SOURCE
, should contain the filename of the called script.
In short, if you’re using a bash-like shell, the preferred way to get the relative path to the current script is by using $BASH_SOURCE
. However, if your system does not have a bash-like shell, use $0
. If you’re wondering why, take a look at the top answers on this slack overflow question: $BASH_SOURCE
vs $0
.
Hence, I highly recommend using the following line to get the relative path to the current script:
SCRIPT_PATH="${BASH_SOURCE:-$0}"
Needless to say, the line above attempts to access the value of $BASH_SOURCE
, and if that is defined in your shell, it will store it in $SCRIPT_PATH
. However, if $BASH_SOURCE
isn’t defined, $SCRIPT_PATH
will contain the value of $0
instead.
Getting The Absolute Path And Directory Of The Shell Script
Considering the example in the previous section, the variable $SCRIPT_PATH
contains the relative path of the current script. Essentially, we will use that variable with the realpath
and dirname
commands to get the absolute path of our script! The perfect way to get the absolute path and directory to a script is shown in the script box below.
SCRIPT_PATH="${BASH_SOURCE:-$0}"
echo "Value of SCRIPT_PATH: ${SCRIPT_PATH}"
ABS_SCRIPT_PATH="$(realpath "${SCRIPT_PATH}")"
echo "Value of ABS_SCRIPT_PATH: ${ABS_SCRIPT_PATH}"
ABS_DIRECTORY="$(dirname "${ABS_SCRIPT_PATH}")"
echo "Value of ABS_DIRECTORY: ${ABS_DIRECTORY}"
Furthermore, when I saved the above block to the file script_dir.sh
in my home directory, and executed it with the following commands:
# make the script executable
chmod +x /home/matheus/script_dir.sh
./home/matheus/script_dir.sh
I got the following output.
$ ./script_dir.sh
Value of SCRIPT_PATH: ./script_dir.sh
Value of ABS_SCRIPT_PATH: /home/matheus/script_dir.sh
Value of ABS_DIRECTORY: /home/matheus
And voilà! You can now use the above lines in your tooling scripts.
Have I missed anything? Do you have any feedback or questions? Have you noticed a mistake/typo? Feel free to comment below and I’ll answer ASAP! Thanks for reading.
Be First to Comment