Live data from Hacker News

$!134 can be a nice bash foot shooter if you accidentally mistype the number

news.ycombinator.com

31–40 of 54 posts

Re: $!134 can be a nice bash foot shooter if you accidentally mistype the number

#32

I don't use the terminal often, but when I do, the feeling of sheer power and control over the system is palpable. I'm a fast and nervous typer, so I always try to slow down and notice all the ways I could lose data, and how easy it would be to make such mistakes (eg. how many mistyped letters away from wiping my database am I?). Over time, I got better at noticing danger areas. Whenever I use 'rm', I re-read the com…

Regarding `rm`, I usually have something like this setup in my bashrc/zshrc: rm () { mv "$@" ~/.trash } So any time I use rm, it actually just moves it to ~/.trash instead. Not fool-proof nor perfect, but have saved me at least once so found it to be good enough for now.

You might want to add a slash at the end of "trash", or add a mkdir before that command. Otherwise, doing this on a system where ~/.trash doesn't exist can still make you lose files after multiple deletions.

Re: $!134 can be a nice bash foot shooter if you accidentally mistype the number

#33
post #18
post #2

Fortunately my memory was never good enough for me to use that facility. C-r is how I always lookup earlier commands.

Can you navigate C-r history, or are you forced to see only the last command?

Pair with `fzf` if you want good ux.

Re: $!134 can be a nice bash foot shooter if you accidentally mistype the number

#34

I don't use the terminal often, but when I do, the feeling of sheer power and control over the system is palpable. I'm a fast and nervous typer, so I always try to slow down and notice all the ways I could lose data, and how easy it would be to make such mistakes (eg. how many mistyped letters away from wiping my database am I?). Over time, I got better at noticing danger areas. Whenever I use 'rm', I re-read the com…

Regarding `rm`, I usually have something like this setup in my bashrc/zshrc: rm () { mv "$@" ~/.trash } So any time I use rm, it actually just moves it to ~/.trash instead. Not fool-proof nor perfect, but have saved me at least once so found it to be good enough for now.

I use the `can` command. It does something similar. Personally, I find a separate command to be better so that I treat rm with kid gloves on new systems. https://github.com/joshvoigts/can

Re: $!134 can be a nice bash foot shooter if you accidentally mistype the number

#35

Earlier quoted context omitted.

> 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.

If you get used to using a different command, you won't use `rm` accidentally

Sure, but in the context of the present thread I'd have to wait until the history file gets rotated - which will never happen on the systems I manage.

Re: $!134 can be a nice bash foot shooter if you accidentally mistype the number

#36

Earlier quoted context omitted.

> 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.

Not necessarily. If you get used to using `delete` all the time, that is most likely what you would use on the server too. Once you trip over it missing, you are reminded that you are in dangerous territory.

There are many assumptions here. I manage several servers, but many of these are co-managed. Second, it is not foolproof - muscle memory is very strong and for sure sooner or later I'll type rm again. Third, it doesn't prevents the use of rm in scripts.

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.

Re: $!134 can be a nice bash foot shooter if you accidentally mistype the number

#37
post #2

Fortunately my memory was never good enough for me to use that facility. C-r is how I always lookup earlier commands.

I do this too, but you know what is an even cooler and more rewarding way to exploit the shell history?

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 ..

Re: $!134 can be a nice bash foot shooter if you accidentally mistype the number

#38
post #2

Fortunately my memory was never good enough for me to use that facility. C-r is how I always lookup earlier commands.

I do this too, but you know what is an even cooler and more rewarding way to exploit the shell history? 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 w…

I have a similar system but take advantage of prefix-search rather than Ctrl+r

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= and recall that command (until it drops off the end of my shell history).

  pin= r curl -fiXPOST "https://ipfs.infura.io:5001/api/v0/pin/add?recursive=true&arg=$(c)"

Re: $!134 can be a nice bash foot shooter if you accidentally mistype the number

#39

I never understood the utility of history substitution (I think this is what it's called). Why not just ctrl-r to see the actual command? Also, `sudo !!` thingy that is (was?) persistent in the internets for some reason, is probably the dumbest thing out there.

I never used !! and similar. However my CLI is set to keep the cursor at the front when pressing to go through history (for easy prefix searching) so I can just use `sudo ` to get the same result and I like seeing the full command before I run it.

Re: $!134 can be a nice bash foot shooter if you accidentally mistype the number

#40

Earlier quoted context omitted.

I do this too, but you know what is an even cooler and more rewarding way to exploit the shell history? 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 w…

I have a similar system but take advantage of prefix-search rather than Ctrl+r 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= and recall that command (until it drops off the end of my shell history). pin= r curl -fiXPOST "https://…

Useful, but dangerous! I would have to check if there is already an environment variable in use before I did that ..
Post reply on HN