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

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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