Exercise 3: A Git-Branch-Aware Prompt Function — Possible Solution ==================================================================== THE FUNCTION ------------------------------ function prompt { $path = $PWD.Path $branch = git branch --show-current 2>$null if ($branch) { "PS $path [$branch]> " } else { "PS $path> " } } HOW IT WORKS ------------------------------ $PWD.Path gives the current working directory, matching this chapter's own basic prompt example. git branch --show-current attempts to read the current git branch name; 2>$null silently discards any error output (relevant when the current folder isn't a git repository at all, which would otherwise print an error to the console every time the prompt is drawn). The if ($branch) check - relying on $branch being $null/empty (and therefore falsy) when there's no git repository - decides which of the two return strings actually becomes the prompt: one with the branch name in brackets when inside a repo, and a plain path-only version otherwise. WHY THIS AVOIDS THE UNCAPTURED-OUTPUT GOTCHA ------------------------------ Both branches return exactly one string each, and no other uncaptured expression exists anywhere in the function body - per this chapter's own warning about Fundamentals 7's rule, this ensures the prompt text is exactly what's intended, with nothing extra silently tacked on. WHY THIS WORKS AS AN ANSWER ------------------------------ It provides a correct, working prompt function that shows the path always and the git branch conditionally, and correctly avoids introducing any stray uncaptured output that could corrupt the displayed prompt text.