Brace and tilde expansion
How it works
Brace expansion generates words from a literal comma list or sequence: item-{a,b} or item-{1..3}. It does not inspect the filesystem. Tilde expansion replaces an eligible unquoted leading ~ with the home directory; ~+ and ~- refer to PWD and OLDPWD when set. ~user looks up a named user. If an eligible tilde prefix cannot be resolved, Bash leaves it unchanged. An unset HOME makes bare ~ use the current user’s home directory from the account database. Quoting the tilde makes it literal. The examples use a synthetic HOME rather than exposing a personal directory.
Run each example separately in a noninteractive Bash 5.2 script in a fresh empty practice directory, with default options and C locale. Output is shown separately; do not enter it as code.
Generate names
Expected stdout:
report-draft.txtreport-final.txtThe list creates two strings regardless of existing files.
Sequences and increments
Expected stdout:
part-02part-04part-06Zero padding and a step of two are part of the sequence syntax.
Home paths
Expected stdout:
/demo/notes~/demo/notesAn eligible unquoted tilde expands; a quoted tilde remains literal.
Common mistakes
Brace expansion needs literal syntax: {1..$end} does not expand into a dynamic sequence. Quoted "~/notes" is not a home path; use "$HOME/notes" when composing a quoted path.
Practice
Generate report-01.txt through report-03.txt without creating files. Then print a home-relative notes path with HOME set to /demo in a practice script.
Next: Command substitution