Linux / Unix: Shell Script Find Out In Which Directory Script File Resides
To find out the directory in which a shell script resides, you can use the dirname command along with the $0 shell variable, which represents the name of the script file.
Here's the command to find the directory of a shell script:
dir=$(dirname "$0")The dirname command will extract the directory portion of the $0 variable, which contains the full path to the script file. The result of the dirname command is stored in the dir variable.
For example:
#!/bin/bash
dir=$(dirname "$0")
echo "The script is located in $dir"This script will print the location of the script file in the terminal.
