Walk upward until a matching parent directory is found.

Section: Files and Temporary Paths

Walk up to repo root

bash
bash
dir=$PWD
while [[ $dir != / && ! -d $dir/.git ]]; do dir=$(dirname "$dir"); done
echo "$dir"
Explanation

Useful for repo-aware scripts.

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 Files and Temporary Paths
Create temp file
Create a secure temporary file.
OpenIn sheetbashsame section
Create temp directory
Create a secure temporary directory.
OpenIn sheetbashsame section
Copy file if contents differ
Avoid replacing files unnecessarily.
OpenIn sheetbashsame section
Check if command exists
Require a dependency before running script logic.
OpenIn sheetbashsame section
Get script directory
Resolve the directory of the current script.
OpenIn sheetbash1 tag match
Portable Bash shebang
Use env to locate Bash on PATH.