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

You can use the `$0` variable to get the directory where the bash script is located from within the script.

For example, if the script is located in `/home/user/scripts/myscript.sh`, you can use the following command to get the directory:

“`
DIR=”$( cd “$( dirname “$0″ )” >/dev/null 2>&1 && pwd )”
“`

This will set the `DIR` variable to `/home/user/scripts`.

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

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?