avatar
Bobby Iliev

Jun 19, 2024

How to Get the Directory Where a Bash Script is Located

How to Get the Directory Where a Bash Script is Located

Get featured on Geeklore.io

Geeklore is in very early stages of development. If you want to help us grow, consider letting us sponsor you by featuring your tool / platform / company here instead of this text. 😊

Hello everyone!

When you're working with Bash scripts, it's often useful to know the directory where the script itself resides. This can be essential for referencing relative paths, ensuring the script works correctly regardless of where it is executed from. Let’s dive into how you can achieve this.

Method 1: Using dirname and $0

The simplest way to get the directory of the script is to use the dirname command in combination with $0. Here's a step-by-step explanation:

  1. $0: This special variable contains the path used to invoke the script. It might be a relative path, an absolute path, or just the script name.
  2. dirname: This command removes the last component from a path, effectively giving you the directory part.

Here’s a small snippet to demonstrate this:

#!/bin/bash

# Get the directory of the script
SCRIPT_DIR=$(dirname "$0")

echo "The script is located in: $SCRIPT_DIR"

Method 2: Resolving the Full Path

If you need the absolute path, especially if the script is run from a relative path, you can combine dirname with readlink:

#!/bin/bash

# Resolve the full path of the script
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")

echo "The script is located in: $SCRIPT_DIR"

Breaking Down the Command

  • readlink -f "$0": This command follows all symlinks and returns the absolute path of the script.
  • dirname "$(readlink -f "$0")": This gives the absolute directory path of the script.

Method 3: Handling Edge Cases

For more complex scenarios, such as when the script is sourced or if it's part of a symlink chain, you might need additional logic to ensure you always get the correct directory.

Here’s an advanced version that handles more edge cases:

#!/bin/bash

# Function to resolve the script path
get_script_dir() {
    local SOURCE=$0
    while [ -h "$SOURCE" ]; do # Resolve $SOURCE until the file is no longer a symlink
        DIR=$(cd -P "$(dirname "$SOURCE")" && pwd)
        SOURCE=$(readlink "$SOURCE")
        [[ $SOURCE != /* ]] && SOURCE=$DIR/$SOURCE # If $SOURCE was a relative symlink, resolve it relative to the symlink base directory
    done
    DIR=$(cd -P "$(dirname "$SOURCE")" && pwd)
    echo "$DIR"
}

# Get the directory
SCRIPT_DIR=$(get_script_dir)

echo "The script is located in: $SCRIPT_DIR"

Conclusion

Getting the directory of the Bash script from within the script itself is a common requirement and can be easily achieved using dirname and $0. For more robust solutions, combining these with readlink can ensure you handle absolute paths and symlinks correctly. By incorporating these methods into your scripts, you can make them more portable and easier to manage.

If you’re interested in learning more about Bash scripting, be sure to check out my free eBook on Bash scripting. It's packed with useful information and examples to help you become a Bash scripting pro!

Happy scripting!

Latest Comments

    avatar Algorithm Adept
    Kuberdenis Algorithm Adept Level 27
    6 months ago

    Thanks for posting, Bobby!

    avatar Code Scribe
    Bobby Iliev Code Scribe Level 8
    Author 6 months ago

    🤜 🤛

More From This User

3 0 57
Read Now
4 0 25
Read Now
4 0 47
Read Now
2 0 16
Read Now
5 0 81
Read Now

© 2024 Geeklore - DEV Community RPG

Facebook Twitter Linkedin Instagram

Campaign Progression Updated!