You can obtain the directory where a Bash script is located from within the script itself by using the $0 parameter, which contains the path to the script that is currently running, and the dirname command, which extracts the directory portion of the path.

You can use the following command to get the directory where a Bash script is located from within the script itself:

#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo "The script is located in the directory: $DIR"


Here's what this command does:
-> ( dirname "${BASH_SOURCE[0]}" ) gets the directory containing the script file itself.
-> cd changes the current directory to the one containing the script file.
-> pwd gets the absolute path of the current directory, which is the directory containing the script file.
-> The result is stored in the $DIR variable, which you can then use in your script.

This command works even if the script is called from another directory or if it is executed through a symbolic link.