Software & AppsOperating SystemLinux

How To Get the Absolute Path of the Current Bash Script

Ubuntu 19

In the world of scripting, it’s often necessary to know the absolute path of the current Bash script. This can be useful in a variety of situations, such as when you need to reference other scripts or files in the same directory. In this article, we will explore several methods to achieve this.

Quick Answer

To get the absolute path of the current Bash script, you can use various methods such as realpath and dirname, readlink and dirname, which and dirname, or built-in Bash commands. Each method has its own advantages and use cases, so choose the one that best suits your needs.

Understanding Absolute Path

Before we delve into the methods, it’s important to understand what an absolute path is. An absolute path is a complete path from the root of the file system to the file or directory in question. It starts with the root directory (/) and ends with the name of the file or directory. This is different from a relative path, which is relative to the current directory.

Using realpath and dirname

The first method we will look at involves the realpath and dirname commands.

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

Here, $0 is a special variable in Bash that contains the name of the script. The realpath command resolves this name to an absolute path, and the dirname command extracts the directory part of this path. The result is assigned to the script_dir variable.

Using readlink and dirname

An alternative to realpath is the readlink command with the -f option.

script_dir=$(dirname "$(readlink -f "$0")")

The readlink -f command performs the same function as realpath, i.e., it resolves $0 to an absolute path. The rest of the command is the same as in the previous method.

Using which and dirname

If your script is located in one of the directories listed in the PATH environment variable, you can use the which command.

script_dir=$(dirname "$(which "$0")")

The which command returns the absolute path of the script, and dirname extracts the directory part of this path.

Using Built-in Bash Commands

The final method we will look at does not rely on external commands like realpath, readlink, or which. Instead, it uses built-in Bash commands.

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

In this method, $PWD is another special variable in Bash that contains the current directory. The case statement checks whether $0 starts with a / (i.e., it’s an absolute path). If it does, it assigns $0 to script_dir. If it doesn’t, it prepends $wdir and removes any leading ./ from $0 before assigning it to script_dir. The final line removes the file part of script_dir, leaving only the directory part.

Conclusion

In this article, we have explored several methods to get the absolute path of the current Bash script. Each method has its own use cases and advantages, so choose the one that best fits your needs. Remember that these methods assume that the script is being executed directly and not sourced by another script. Happy scripting!

What is the purpose of knowing the absolute path of the current Bash script?

The absolute path of the current Bash script is useful for referencing other scripts or files in the same directory, regardless of the current working directory. It provides a fixed location for accessing resources.

What is the difference between an absolute path and a relative path?

An absolute path is a complete path from the root of the file system to the file or directory, starting with the root directory ("/"). A relative path, on the other hand, is relative to the current directory and does not begin with the root directory.

How does the `realpath` command work?

The realpath command resolves the name of the script ($0) to an absolute path by traversing symbolic links and resolving relative paths. It provides the actual physical path of the script, even if it was executed via a symbolic link.

What is the purpose of the `dirname` command?

The dirname command extracts the directory part of a given path. It is used in combination with commands like realpath or readlink to obtain the directory path of the current script.

How does the `which` command help in obtaining the absolute path of the current script?

The which command searches for the specified command or script in the directories listed in the PATH environment variable. By passing $0 to which, it returns the absolute path of the script if it is located in one of those directories.

Can the built-in Bash commands be used to obtain the absolute path of the current script?

Yes, the built-in Bash commands can be used to obtain the absolute path of the current script. By using variables like $0 and $PWD, along with string manipulation and conditional statements, it is possible to derive the absolute path without relying on external commands like realpath or readlink.

Are these methods applicable only when executing the script directly and not when sourced by another script?

Yes, these methods assume that the script is being executed directly. When a script is sourced by another script using the source or . command, the value of $0 may not reflect the actual script being sourced.

Leave a Comment

Your email address will not be published. Required fields are marked *