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

Bash
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:

Bash
mkdir -p myproject/{src,tests,docs}

That's three folders in one go. Some more:

Bash
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

Bash
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

Bash
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

Bash
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

Bash
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:

Bash
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."

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:

Bash
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

TrickWhat 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^newFix a typo and rerun
cd -Go back to the previous directory
Ctrl-X Ctrl-EEdit 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.