Live data from Hacker News

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

news.ycombinator.com

11–20 of 54 posts

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

#11

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.

`sudo !!` is just very useful, just as most other things you can do with history substitution. Ctrl-R has its own usefulness but it only does a fraction of what history substitution can do.

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

#12
post #10

You need to enable magic-space: https://relentlesscoding.com/posts/bash-magic-space/ # this will automatically expand any variables and history when you hit the spacebar after them, so you can eyeball before running. Someone tell me what the zsh equivalent is?

In zsh you just type !1234 and it expands to the full command so you can review and edit :D

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

#13
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've used build-in browser for years, but I highly recommend switching to https://github.com/junegunn/fzf for C-r in shell. Night and day boost.

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

#14

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.

`sudo !!` is just very useful, just as most other things you can do with history substitution. Ctrl-R has its own usefulness but it only does a fraction of what history substitution can do.

I like fish shell's approach: [Alt]+[E] prepends the command with `sudo`.

So you hit arrow up to bring up the last command, then [Alt]+[E] to sudo-ify it.

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

#15

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.

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

#17

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.

`sudo !!` is perfectly alright. It references the very last command, which in all likelihood is right there on your screen, typed by you a few seconds ago, and you're aware of the context.

$!(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.

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

#20
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?

You can! If you do C-r again, then it will show the next command in your history for the given search.
Post reply on HN