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: Alt-# comments out the bash line and saves it in history

Bash
~/tips/tip-bash-alt-hash-comment-line

#rsync -av --delete /srv/media/ /mnt/backup/media/

Park a half-written command in history without running it.

Halfway through a long command it can turn out you need to check something first. Deleting the line loses the work, and Ctrl-c does too. Alt-# keeps it, which I find the neatest way out.

Press Alt-# (or Esc then #) anywhere on the line. Bash puts a # in front and accepts the line as if you pressed Enter:

$ #rsync -av --delete /srv/media/ /mnt/backup/media/

Since it starts with #, the shell treats it as a comment and runs nothing. But it is now in your history. Check what you needed, press Up to get the line back, remove the # with Ctrl-a and Delete, and run it.

There is a nice extra: with a numeric argument, like Alt-1 and then Alt-#, it works as a toggle, so on a line that already starts with # bash removes the comment and runs the line.

In a script history is not involved, so this is for the interactive prompt only. Watch out with commands that hold a password, because the commented line lands in your history file just like any other. On a Mac, the Option key has to act as Meta in your terminal settings for Alt-#; the Esc then # version always works.

Further reading: Bash manual: Miscellaneous Commands (insert-comment)

More Bash tips echo {01..10} cmd > file 2>&1 export HISTIGNORE='ls*:pwd:cd*' APP_HOST=example.com set -e All Bash tips (52) All topics

Link