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: !cmd:p shows what bash would rerun without running it

Bash
~/tips/tip-bash-history-p-print-only

!rsync:p

Preview a history expansion before you run it, then press Up to use it.

!rsync reruns the last command that started with rsync, which is quick and a bit scary when you do not remember which one that was. The :p modifier prints it instead.

Add :p to any history expansion:

!rsync:p
# rsync -av --delete /srv/media/ /mnt/backup/media/

Nothing runs. Bash prints the command and also puts it in your history as the newest entry, so you can press Up to get it on the line, check it or edit it, and then press Enter.

It combines with the other modifiers. !ls:s/hostname/hosts/:p shows the last ls command with hostname replaced by hosts, again without running it. !!:p prints the previous command.

I like :p more than the histverify option when I only want a preview once in a while, since it does not change how ! works the rest of the time.

History expansion only exists in interactive shells, so none of this works inside scripts. And ! is greedy: !rsync matches the latest command starting with those letters, also an rsync --help you typed later. That is exactly why it pays to check it first.

Further reading: Bash manual: History Expansion (Modifiers), Bash manual: Event Designators

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