How to Find the Directory of a Bash Script Using the Same Script?

As a Linux user, it’s common to have several bash scripts stored in different directories. Sometimes, a user might want to know the exact directory where a particular script is located. This is especially useful when a user wants to call another script or access some files in the same directory. 

In this article, different methods will be discussed that can be used to locate the bash script location. These methods are listed below.

  • Using the $0 Variable
  • Using the PWD Command
  • Using the Bash SOURCE Command
  • Using the find Command
  • Using the locate Command

Method 1: Using the $0 Variable

The easiest way to find the directory where a Bash script is located within the script is to use the $0 variable. The $0 variable contains the script’s name itself, including the path. To extract the directory from the $0 variable, we can use the dirname command. Here’s an example:

#!/bin/bash
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
echo "The directory where this script is located is: $SCRIPT_DIR"

Code Explanation:

  • The ‘$(…)’ is a command substitution that runs the command inside the parentheses and replaces the command with its output.
  • The ‘readlink -f $0’ returns the absolute path of the script’s location, whereas the ‘dirname’ returns the parent directory of the path passed to it.
  • The result will be stored in the ‘SCRIPT_DIR’ variable and printed on the terminal.

The output of the bash script can be seen by executing the bash script below:

$ bash find_dir.sh

Method 2: Using the PWD Command

The PWD command can also be used to find the directory where a Bash script is located within the script. The PWD command stands for “print working directory” and the below bash script can be used for this purpose:

#!/bin/bash
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
echo "The directory where this script is located is: $SCRIPT_DIR"

Code Explanation:

  • The ‘$(…)’ is a command substitution that runs the enclosed command and replaces it with the output of that command.
  • The ‘cd “$(dirname “$0”)”‘ changes the working directory to the directory of the script, whereas the ‘pwd’ prints the current working directory.
  • The final result of this command substitution will be assigned to the ‘SCRIPT_DIR’ variable.

The output of the bash script can be seen by executing the bash script below:

$ bash find_dir.sh

Method 3: Using the source Command

The Bash source command can also be used to find the directory where a Bash script is located from within the script, and one of its examples is mentioned below:

#!/bin/bash
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
echo "The directory where this script is located is: $SCRIPT_DIR"

Code Explanation:

  • The ‘${BASH_SOURCE[0]}’ variable is used to get the path to the script. 
  • The ‘dirname’ command is used to extract the directory, and the ‘pwd’ command is used to get the absolute path of the directory. 
  • The result is stored in the ‘SCRIPT_DIR’ variable and printed on the terminal.

The bash source command can also be utilized in another way to the bash script location, which is mentioned below

#!/bin/bash
SCRIPT_DIR="$( dirname "${BASH_SOURCE[0]}" )"
echo "The directory of the script is: $SCRIPT_DIR"

In the above example, the dirname command is being used to extract the directory path of the script from the ‘${BASH_SOURCE[0]}’ variable.

The execution of both the bash script will be the same, which can be verified by executing the below script

$ bash find_dir.sh

Method 4: Using the find command

The find command in Linux searches for files in a directory hierarchy, and we can use this command to search for the script file and then extract its directory path. Here’s an example:

#!/bin/bash
SCRIPT_DIR="$( dirname "$( find "$( pwd )" -name "$( basename "${BASH_SOURCE[0]}" )" -print -quit )" )"
echo "The directory of the script is: $SCRIPT_DIR"

Code Explanation:

  • The ‘find’ command is being used to search for the script file in the current directory and its subdirectories. 
  • The ‘-name’ option is used to specify the name of the script file, and the ‘-print’ option prints the file path. 
  • The ‘-quit’ option to stop searching after finding the first occurrence of the script file, and then using the dirname command to extract the directory path.
  • The result is stored in the ‘SCRIPT_DIR’ variable and printed on the terminal.

The output can be seen after the execution of the bash script as below:

$ bash find_dir.sh

Method 5: Using the locate Command

The locate command in Linux searches for files in a database of file names and their paths. We can use this command to search for the script file and then extract its directory path. Here’s an example:

#!/bin/bash
SCRIPT_DIR="$( dirname "$( locate -n 1 "$( basename "${BASH_SOURCE[0]}" )" )" )"
echo "The directory of the script is: $SCRIPT_DIR"

 Code Explanation:

  • The ‘locate’ command is being used to search for the script file in the database. 
  • The ‘-n’ option limits the search results to the first occurrence of the file.
  • The ‘dirname’ command is used to extract the directory path.

The output can be seen after the execution of the bash script as below:

$ bash find_dir.sh

Conclusion

Sometimes, it is very important to find a directory where a certain bash script is located. One of its examples could be if a user wants to call a bash script to another, then its exact location is a crucial factor. In this article, five different methods have been discussed, which are Using the $0 Variable, Using the PWD, Bash source, find and locating Commands.