Skip to content
Go back

Linux Bash Tips and Tricks pt1

· Updated:
By SumGuy 4 min read
Linux Bash Tips and Tricks pt1

Bash is a powerful command-line shell that can be used to perform a wide range of tasks, from managing files and directories to automating system administration. By learning a few simple tips and tricks, you can significantly boost your productivity and efficiency when using Bash.

Here are a few of my favorite Bash tips and tricks, in a randomized order:

Where These Tricks Actually Break

Here’s the thing nobody puts in the tips post: half of these will silently not work depending on how your shell is configured, and you’ll spend 20 minutes convinced you’re doing it wrong.

!! and history expansion don’t work in scripts. They’re interactive-only. If you paste sudo !! into a script, you’re not running the last command — you’re passing a literal !! to sudo, which will do nothing useful and complain loudly about it.

histverify and histreedit need HISTFILE to be set. If you’re on a machine where history is disabled (some hardened servers, CI containers), those shopt options load fine but do absolutely nothing. Silent failure. Love that for us.

Ctrl+R exits with the wrong binding in some terminals. If Ctrl+R opens reverse-i-search but Ctrl+G doesn’t cancel it, your $TERM or readline bindings are off. Add this to ~/.inputrc to normalize it:

Terminal window
"\C-g": abort
"\C-r": reverse-search-history

Esc-. breaks if you’re in vi mode. Bash supports both emacs and vi keybindings. Esc-. is an emacs binding. If you or some tool ran set -o vi, you’re now in vi insert mode and Esc drops you to command mode — . there repeats the last edit, not the last argument. Check with set -o | grep -E 'vi|emacs'.

The trap Gotcha That’ll Bite You

trap is great for cleanup, but there’s a common footgun: if you define your trap after a subshell launches, the subshell doesn’t inherit it. Traps are not exported.

Terminal window
#!/usr/bin/env bash
# This works
trap 'rm -f /tmp/lockfile' EXIT
# This does NOT propagate the trap into the subshell
(
echo "I'm a subshell, trap won't run here on exit"
exit 1
)

If your cleanup logic needs to run inside a subshell too, define the trap inside it. Or better, don’t rely on subshell traps for anything critical — use a top-level trap and clean up by reference after the subshell returns.

The shortcuts are real time-savers once they’re in muscle memory. Just don’t assume any of them work everywhere until you’ve confirmed your environment actually has them set up.


Share this post on:

Send a Webmention

Written about this post on your own site? Send a webmention and it'll show up above once verified.


Previous Post
LinkedIn Is Searching Your Computer
Next Post
Linux distribution info & kernel info

Discussion

Powered by Garrul . Sign in with GitHub or Google, or post anonymously.

Related Posts