Dualo
Bash / Shell Scripting

Globbing & expansions

Wildcards for filenames, brace expansion for lists, parameter expansion for string ops. The shell rewrites your command line before running it.

1 min read

Before running a command, the shell rewrites your line through several expansion phases. Understanding the order (brace → tilde → variables → command substitution → arithmetic → word splitting → pathname) prevents surprises.

Glob patterns (pathname expansion): * matches any characters (not /), ? matches exactly one char, [abc] matches one of a set, [0-9] matches a range. ls *.log expands to a literal list of matching files. If nothing matches, the glob stays literal by default (you'll get ls: *.log: No such file or directory).

Brace expansion {a,b,c}: generates a list without touching the filesystem. echo {red,green,blue}.txt prints red.txt green.txt blue.txt. Ranges: {1..5}, {a..e}, {01..10} (zero-padded). Useful for one-line multi-file operations: cp file.txt{,.bak}cp file.txt file.txt.bak.

Tilde expansion: ~ → your home directory. ~user → that user's home. ~-$OLDPWD. ~+$PWD. Only works at the start of a word, not in the middle — /opt/~/thing stays literal.

Parameter expansion tricks: ${var:-default} (fallback), ${#var} (length), ${var#prefix} (strip prefix), ${var%suffix} (strip suffix), ${var/old/new} (replace). These are pure Bash — no external sed/cut, no fork.

Grounded on https://www.gnu.org/software/bash/manual/bash.html#Shell-Expansions

Next up

Text processing — grep, sed, awk, cut, sort, uniq

The Unix data-wrangling toolkit. Compose these with pipes and you solve 90% of log/CSV/text tasks without writing a real program.