brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: shopt -s histverify shows what !! or !$ expands to before bash runs it

Bash
~/tips/tip-bash-histverify

shopt -s histverify

Puts the result of !! or !$ on your command line first, so you check it before it runs.

History expansion is fast: !! for the last command, !$ for its last argument, !rm for the last command that started with rm. It is also a little blind, because bash runs the result the moment you press Enter. I like a moment to look first.

That is what the histverify shell option does. With it on, bash does not pass the expanded line straight to the parser. It puts the result back on your command line so you can read it, edit it, and press Enter again when you are happy.

shopt -s histverify

Put that line in ~/.bashrc to keep it. A quick example:

$ ls /mnt/user/appdata/jellyfin
$ cd !$
$ cd /mnt/user/appdata/jellyfin     # shown, not run yet

After the first Enter you see the full command with the path filled in. Press Enter a second time to run it, or change it first. Where I’d really want this is with the riskier patterns, like !rm or !?docker?, where you are trusting your memory of what the last match was. With histverify you see the exact rm line before it can do anything.

The caveat is the extra Enter. If you use sudo !! all day, every one of those now takes two presses, and that can get old fast. It also only works when readline is in use, so it does nothing in a script, where history expansion is off anyway. If two presses feel like too much, the :p modifier is the one-off version: !rm:p prints the expanded command and adds it to history without running it.

Further reading: The shopt builtin (GNU Bash manual), Using history interactively (GNU Bash manual)

More Bash tips echo {01..10} cmd > file 2>&1 export HISTIGNORE='ls*:pwd:cd*' #rsync -av --delete /srv/media/ /mnt/backup/media/ APP_HOST=example.com All Bash tips (52) All topics

Link