Get Script Directory in Bash

Get Script Directory in Bash

Using BASH_SOURCE Array

To get the current script’s directory in a Bash, use the BASH_SOURCE array with pwd command.

In the above example, the BASH_SOURCE variable is used with dirname to get the path of executing the script directory. In Bash, the BASH_SOURCE is an array variable containing the list of source file names associated with each script level. Here, dirname is used to extract the directory path of the script. Then, the cd command is changed to that directory.

In BASH_SOURCE[0], the element at the 0 index represents the first element in the array which is the file’s name currently executing. Here, >/dev/null 2>&1 is used to prevent the error messages, if any and the pwd command printed the path of the working directory, respectively.

In Bash, >/dev/null 2>& 1 redirects the error message to the null device.

We can cross-check what is at the 0 index in the BASH_SOURCE array using the following code:

We can verify the value at the first index; we got ./BashScript.sh, the basename of the currently executing script.

Using $0 Variable

To get the current script’s directory in a Bash, use the $0 array variable. In Bash, the $0 variable refers to the script’s name or shell being executed

In Bash, the $0 variable refers to the script’s name or shell being executed. For example, we used the $0 with dirname to get the current script’s directory.

After executing the above code directory path of the BashScript.sh file is retrieved as /c/Users/John/Desktop/bashFiles. The $0 variable can be helpful for various purposes, such as printing the script name in a log or using it to construct paths relative to the script location.

Using realpath

To get the current script’s directory in a Bash, use the realpath with $0. In Bash, the $0 variable refers to the script’s name or shell being executed

In Bash, the realpath is used to get the absolute path of the script or a directory. Now, what is the absolute path? There are two types of paths:

  • Absolute Path: It retrieves the script’s full path starting from the file system’s root. For example: If our current script is BashScript.sh, our relative path of the script will be /c/Users/John/Desktop/bashFiles/BashScript.sh

  • Relative Path: It retrieves the directory’s full path from the current working directory instead of the file system’s root. For example, if our current working directory is /c/Users/John/Desktop/, our relative path will be bashFiles/BashScript.sh.

Here, $(realpath "$0") is used to get the absolute path of our script which is /c/Users/John/Desktop/bashFiles/BashScript.sh as shown above. After that dirname method is used to retrieve the directory path from the absolute path.

The dirname omits the file name from the end of the absolute path to get the directory path.

Using readlink

To get the current script’s directory in a Bash, use the readlink with $0. In Bash, the $0 variable refers to the script’s name or shell being executed

In the above bash script, the readlink retrieves the absolute path of the BashScript.sh script, which is /c/Users/John/Desktop/bashFiles/BashScript.sh. Then, dirname retrieved the script directory /c/Users/John/Desktop/bashFiles. In Bash, readlink displays the value of symbolic links or canonical file names.

Now, what are the symbolic links and canonical file names? In LINUX / UNIX, a symbolic link is like a shortcut to a file or a directory in the windows operating system. It is used as a reference for another file or a folder in other parts of the file system. These are also known as soft links. A canonical file name refers to the absolute path of a file or directory in the file system. It is the real path of the file or a directory, not a shortcut pointing to another file or a directory like a symbolic link.

The -f parameter follows all the symbolic links in the given path and prints the name of the canonical file the path refers to.

That’s all about how to get script directory in Bash.

Was this post helpful?

Leave a Reply

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