Live data from Hacker News

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

news.ycombinator.com

41–50 of 54 posts

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

#41

Earlier quoted context omitted.

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

I know this command doesn't depend on a lowercase `pin` variable so there is no danger. Note that this doesn't change the variable in the shell, just the executed command.

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

#42

Earlier quoted context omitted.

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

I know this command doesn't depend on a lowercase `pin` variable so there is no danger. Note that this doesn't change the variable in the shell, just the executed command.

Yeah, its the 'knowing beforehand' part that makes this dangerous for me. ;)

Cute trick though, will work it into my workflow and see how it feels.

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

#43

Earlier quoted context omitted.

I know this command doesn't depend on a lowercase `pin` variable so there is no danger. Note that this doesn't change the variable in the shell, just the executed command.

Yeah, its the 'knowing beforehand' part that makes this dangerous for me. ;) Cute trick though, will work it into my workflow and see how it feels.

IDK, environment variables get set all of the time, I rarely worry about them breaking programs. Their job is kinda to live in the background and be passed through programs so it doesn't feel like much danger. Especially when I use lowercase and almost all programs use uppercase.

But I guess everyone has a different risk level and what feels near-zero to me is meaningful to someone else.

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

#44

Earlier quoted context omitted.

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.

just grep through your history file and delete lines containing `rm` easy fix

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

#45
You can view the history (with the line numbers) by typing 'history'. If you have the following set, you can avoid any command prefixed with a space ever getting in this list. HISTCONTROL=ignorespace

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!

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

#48
post #22
post #2

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

https://github.com/cantino/mcfly Life changer

Agreed. Much better than Bash's terrible built in ctrl-R which doesn't even show what you're typing if it doesn't match.

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

#49
post #5
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 always add "\e[A": history-search-backward "\e[B": history-search-forward to my .inputrc, I find it easier to use than C-r as it's some sort of autocomplete from history. But looking up commands with !number can still be very useful when you don't remember the command you need but remember when you used it (e.g. what commands you ran before and after).

What are the "\e[A" and "\e[B" do, are they key presses?

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

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

#50

If your are on a production server, it's more like a Russian roulette rather than lottery.

Real Russian roulette would be to preload history with a sure-shot such as `sudo rm -rf /`

If you don't like your job there's also the Polish variant, where you take one bullet out of every six.

Post reply on HN