Bash Party Tricks: Brace Expansion, !! and Other Shortcuts That Make You Look Like a Wizard
Ever typed a long command, hit Enter, and got "Permission denied"? There's a two-character fix for that, and a dozen more bash shortcuts that turn a minute of typing into a second. Party tricks, but useful ones.
Tux Harlow
4 min read
You type a long, beautiful command. Thirty characters, perfectly spelled. You press Enter like a concert pianist hitting the final note.
Permission denied
The silence is deafening. [Cut to a concert pianist mid-bow. The piano lid slams on his fingers. The audience politely claps anyway.]
You could retype the whole thing with sudo in front. Or you could learn a few bash shortcuts and fix it in two characters. Here are my favourite party tricks. They're mostly bash, and most of them work in zsh too.
sudo !! — the classic
apt install htop
# Permission denied, you forgot sudo
sudo !!
!! means "the whole previous command". Bash expands it and shows you what it's running, so you can see it happen. This is the single most satisfying thing you can type in a terminal. Do it wrong once on purpose today, just to feel the power.
Brace expansion: make ten things at once
Braces turn one word into many:
mkdir -p myproject/{src,tests,docs}
That's three folders in one go. Some more:
cp nginx.conf{,.bak} # cp nginx.conf nginx.conf.bak
mv notes.{txt,md} # mv notes.txt notes.md
echo {1..5} # 1 2 3 4 5
touch log{01..10}.txt # log01.txt ... log10.txt
echo {a..e} # a b c d e
echo {0..20..5} # 0 5 10 15 20
The {,.bak} one is pure gold: "the file name, then the file name with .bak". It's the fastest backup ever made, and the one you'll be thankful for when the config edit goes sideways.
Not sure what a brace will turn into? Put echo in front first. Brace expansion happens before the command runs, so echo shows exactly what would run. Look before you leap.
!$ and Alt-. — the last argument
mkdir /srv/www/new-site
cd !$
!$ is the last argument of the previous command. Even better is Alt-. (Alt and full stop). It pastes the last argument right onto your prompt, where you can see and edit it. Press it again and it cycles back through earlier commands' last arguments. Think of it as a time machine for file paths.
^typo^fix — quick correction
systemctl restart ngnix
# Unit ngnix.service not found. Yes, we've all done it.
^ngnix^nginx
That reruns the previous command with the first "ngnix" swapped for "nginx". It's a spell-checker, but you're the dictionary.
cd - — the "go back" button
cd /etc/nginx/sites-available
cd /var/log/nginx
cd - # back to sites-available
cd - # and back to the logs
cd - toggles between the last two directories. Ping-pong for people who live in the terminal.
Ctrl-X Ctrl-E: escape to a real editor
You're writing a command so long it's basically a novel, and you've got a typo 80 characters back. Press Ctrl-X, then Ctrl-E. The command opens in your editor (whatever $EDITOR is). Fix it, save, quit, and it runs. For long pipelines, this is life-changing.
Process substitution: diff two commands
diff <(sort old-users.txt) <(sort new-users.txt)
<( ... ) turns a command's output into something that looks like a file. Here, diff compares two sorted lists without you ever creating temp files. Another favourite:
diff <(ssh web1 cat /etc/nginx/nginx.conf) <(ssh web2 cat /etc/nginx/nginx.conf)
That's "why do these two servers behave differently?" answered in one line. You'll feel like a detective. Possibly a wizard detective. Harry Potter meets Columbo. "Just one more thing... Expelliarmus."
Ctrl-R: your history search
Press Ctrl-R and start typing to search everything you've ever run. If you've installed fzf (see our fzf post), it turns into a full fuzzy finder. If you haven't, it's still a good deal better than tapping the up arrow until your finger falls off.
A word of caution
!! and its friends run immediately, with no "are you sure?". sudo !! after rm -rf something will do exactly what you asked. Bash's histverify option shows the expanded command and waits for Enter before running it:
shopt -s histverify # put it in ~/.bashrc
Now !! expands onto the prompt, and you press Enter when you're happy. Your future self thanks you. Your past self, the one who didn't do this, sends his regards from the backup restore.
Cheat sheet
| Trick | What it does |
|---|---|
sudo !! | Rerun the last command with sudo |
cp f{,.bak} | Copy a file to f.bak |
{1..10} | Count, in brace form |
Alt-. | Paste the last argument |
^old^new | Fix a typo and rerun |
cd - | Go back to the previous directory |
Ctrl-X Ctrl-E | Edit the command in your editor |
<(cmd) | Treat output as a file |
Learn one a day. By next week you'll be the person everyone watches over the shoulder of, asking "wait, how did you do that?"
Tell them it's magic. Don't tell them it's braces.
Linux
More like this
Linux
entr and watch: Make Your Terminal Rerun Things So You Don't Have To
Save, switch window, press up, press Enter, squint. Save, switch window, press up... Stop. entr reruns your command whenever a file changes, and watch reruns it on a timer. Two tiny tools, a thousand saved keystrokes.
Linux
ripgrep: grep, But It Drank Three Espressos and Read Your .gitignore
grep is a legend. It's also happy to search all 80,000 files in node_modules while you wait. ripgrep searches only what matters, very quickly, with sensible defaults. The flags worth knowing, and when old grep still wins.
Linux
Linux Terminal Toys (and the Surprisingly Useful Tools Hiding Among Them)
A talking cow, a train that punishes typos, the Matrix in your terminal, and hidden among them, six genuinely useful tools you'll keep long after the novelty wears off. A toy box for Linux nerds.