How to Get Directory Where Bash Script is Located From Within the Script

How to Get Directory Where Bash Script is Located From Within the Script

When writing Bash scripts, it’s often necessary to access the directory where the script is located from within the script itself. This is particularly useful when working with relative paths or when executing other scripts located in the same directory as the current script.

In this tutorial, you will explore different ways to get the directory where a Bash script is located from within the script itself.

How to Get a Directory Where Bash Script is Located From Within the Script

By using the following techniques, you can get the directory where a Bash script is located from within the script itself:

  • Using the “$0” variable
  • Using the “${BASH_SOURCE[0]}” variable
  • Using the “realpath” command

Using the “$0” variable

One way to get the directory where a Bash script is located is to use the “$0” variable. This variable contains the name of the script that is currently being executed, along with its path.

To extract the directory where the script is located, you can use the “dirname” command. Here’s an example:

#!/bin/bash

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

echo "The directory where the script is located is: $SCRIPT_DIR"

In this example, you are using the “readlink” command to get the absolute path of the script, and then using the “dirname” command to extract the directory name.

Using the “${BASH_SOURCE[0]}” variable

Another way to get the directory where a Bash script is located is to use the “${BASH_SOURCE[0]}” variable. This variable contains the name of the current script, along with its path.

To extract the directory where the script is located, you can use the “cd” command to change the current directory to the script’s directory, and then use the “pwd” command to get the absolute path of the directory. Here’s an example:

#!/bin/bash

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

echo "The directory where the script is located is: $SCRIPT_DIR"

In this example, you are using the “cd” command to change the current directory to the script’s directory, and then using the “pwd” command to get the absolute path of the directory.

Using the “realpath” command

Finally, you can also use the “realpath” command to get the absolute path of the script, and then use the “dirname” command to extract the directory name. Here’s an example:

#!/bin/bash

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

echo "The directory where the script is located is: $SCRIPT_DIR"

In this example, you are using the “realpath” command to get the absolute path of the script, and then using the “dirname” command to extract the directory name.

Conclusion

In conclusion, there are different ways to get the directory where a Bash script is located from within the script itself. You can use the “$0” variable, the “${BASH_SOURCE[0]}” variable, or the “realpath” command to get the absolute path of the script, and then use the “dirname” command to extract the directory name. By using these techniques, you can make your Bash scripts more flexible and easier to maintain.

Recommended Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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