Resolve the directory of the current script.

Section: Script Structure

Get script directory

bash
bash
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
Explanation

Useful for loading sibling files and assets reliably.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Script Structure
Portable Bash shebang
Use env to locate Bash on PATH.
OpenIn sheetbashsame section
Parse flags with while/case
Implement manual flag parsing in Bash.
OpenIn sheetbashsame section
Parse short options with getopts
Parse short options using the getopts builtin.
OpenIn sheetbashsame section
Shift parsed options away
Drop parsed options after getopts completes.
OpenIn sheetbashsame section
Walk up to repo root
Walk upward until a matching parent directory is found.
OpenIn sheetbash1 tag match
Create temp file
Create a secure temporary file.