$!134 can be a nice bash foot shooter if you accidentally mistype the number
I'm coding for a few years now, but today it was the first time I mistyped
$!<number>
to repeat a command from the bash history. Fortunately it only launched a Python server. But finally you could accidentally hit any command from your past. It's like playing the lottery :-P
53 comments
[ 660 ms ] story [ 2241 ms ] threadI usually do `history | grep ...` to find the command and then use it.
Non-emacs users might tend to find this surprising, but in a shell buffer you can move around with your cursor like in a text file, so after executing `history` you can search for the command as string then copy-paste it.
Search the command history
* Search as you type: Ctrl + r and type the search term; Repeat Ctrl + r to loop through results.
* Search the last remembered search term: Ctrl + r twice.
* End the search at current history entry: Ctrl + j
* Cancel the search and restore original line: Ctrl + g
Try of course at your own risk. But works for me.
Life changer
By COMMENTING your commands:
$ grep -i myname /etc/passwd # passgrep
.. ..
$ Ctrl-R "passgr"
It is so rewarding to do some intense bash work with liberal command line comments and then come back a few weeks later, "history | grep \#", and see my work notes - along with the relevant commands - available.
I used to write '~/bin' scripts for special stuff, including comments in the .sh source itself - but now it just makes more sense to wang a comment at the end of the complex/interesting commands and then just grep for the comment later ..
For commands that I reuse semi-frequently I set an environment variable so that I can easily find them with prefix searching. So for example I sometimes pin IPFS paths to Infura so I have this command in my history. I can then type pin=<Up> and recall that command (until it drops off the end of my shell history).
Cute trick though, will work it into my workflow and see how it feels.
But I guess everyone has a different risk level and what feels near-zero to me is meaningful to someone else.
If you don't like your job there's also the Polish variant, where you take one bullet out of every six.
Over time, I got better at noticing danger areas. Whenever I use 'rm', I re-read the command a couple of times as an extra precaution. Doesn't feel like that's enough.
I wish we had --dry-run for everything.
Maybe it'd be best to just give this command a new name like `delete` or `trash` just to be safe.
But this would defeat the original purpose, i.e. the prevention of someone accidentally using rm.
That said, an accidental removal of files has never been a problem for me (an accidental chmod -R caused me more woes once), and these days with snapshots it's a complete non issue.
So you hit arrow up to bring up the last command, then [Alt]+[E] to sudo-ify it.
$!(some number) instead is quite insane and much more likely to have unwanted effects due to (potential) lack of context.
Don't conflate the two.
Someone tell me what the zsh equivalent is?
export HISTIGNORE="* rm *"
makes calls to rm not show up in the history, for better or worse.
export HISTIGNORE="* rm *"
export HISTIGNORE="rm *"
I think it's even a bit better. Better preventing worst case if possible right from the start. And usually you don't need rm commands within your history. At least I don't.
I have trained myself, over many years, to hit space automatically before I type rm/reboot/shutdown etc. It's occasionally inconvenient if I need to rerun something but does mean I can't fat-finger anything destructive!
Well the one I use anyway.
For those who want to read about shopt, check:
http://www.gnu.org/software/bash/manual/bash.html#The-Shopt-...
Took me a bit to find it, and there seems to be no man entry.
Most important commands:
shopt -s histverify # activates the option
shopt -u histverify # deactivates the option
shopt # shows all available options and their activation status
You must put "shopt -s histverify" to your .bashrc so that it stays activated.