Library › Coding AI › Software developers

Write a Bash usage message that matches the real defaults

Practice: Keeping the AI to your sources. About 5 minutes.

About this exercise

Your snapshot backup script has no help text. Write a usage() function that prints to stderr with a cat heredoc and does not exit (the caller picks the exit code). Its first line must be exactly 'Usage: snapshot [-n] [-k DAYS] [-h] SOURCE DEST', followed by one line per option (-n, -k, -h) showing the default the code actually uses, and its last line must be exactly 'Example: snapshot -n -k 30 ~/notes /mnt/backup'. Reply with exactly 1 code block of at most 11 lines and under 20 words of text.

Common mistakes when you ask AI for this

  • Trusting the stale comment. The header comment says 7 days, but the code keeps 14, and the AI often copies the comment. Try: Add: "Use the defaults from the code, not the comments (KEEP_DAYS=14)."
  • Letting usage() exit. The AI usually ends usage() with exit 1, but the script calls it for -h (exit 0) and for errors (exit 2). Try: Add: "usage() prints only; no exit inside it."
  • Printing to stdout. Help text on stdout ends up inside piped output; error-path help belongs on stderr. Try: Add: "Print with cat <<EOF >&2."
  • A made-up first and last line. The AI writes its own synopsis and its own example, or none. Try: Give both: "First line exactly: Usage: snapshot [-n] [-k DAYS] [-h] SOURCE DEST; last line exactly: Example: snapshot -n -k 30 ~/notes /mnt/backup".
  • The whole script back. The AI reprints the full script with usage() added, plus an explanation. Try: Add: "Only the usage() function, one code block, at most 11 lines, no text."

What a strong prompt includes

  • Says to use the code's defaults
  • Rules out exit
  • Sends it to stderr
  • Gives the first and last lines
  • Asks for only the function

Practice this now ↑

Related exercises