I use zsh (with oh-my-zsh) and when I type e.g. "sudo !!" and press enter, it doesn't execute the command, instead it expands the !!. Another enter will then actually execute it.
I believe it's default behavior for oh-my-zsh. At least, I don't recall manually changing this.
I don't use history expansions, though, so YMMV
sudo !!:p
which tells bash to do the history expansion, but print the result and add it to the command line history, instead of actually running it. If the command looks correct, it's only two more keystrokes (up-arrow, enter) to run it for real.I think this not just "partially helps", I think it completely addresses the problem described by the above commenter. Perhaps s/he is not aware of this feature.
history-expand-line is documented in: https://www.gnu.org/software/bash/manual/html_node/Miscellan...
shopt -s histverify
shopt -s histreedit
If set, and readline is being used, a user is given the opportunity to re-edit a failed history substitution.Not even "sudo !!" ?
You can see the command you just typed in the line above, and you just forgot to add sudo to it.
Another way of putting it is that I'll gladly press another 2-3 keys if it prevents a class or errors and also reduces the number of special bash instructions I have to keep in memory. !! isn't even in my repertoire, but I don't feel its loss.
For example, say you want to run "chown root:root foo". Alt-. gets you "foo", not the entire command, and "sudo foo" isn't useful,and in some cases could do something else entirely. For example, with the command "service foo restart", you would end up with "sudo restart" (which while it doesn't execute right away, is exactly what I'm guarding against).
Unlike Emacs, M-1 etc. can't be typed as C-u 1 etc.
$ yum install bash-completion
Error: This command has to be run under the root user.
$ sudo <Alt-.>
$ sudo bash-completionBut I'm an epic coward that even looks both to the left and the right before crossing a street. :-)
#!/bin/bash
echo -n "Are you SURE you dumbass? [N/y] " >/dev/stderr
read -n 1
if [ "$REPLY" = "y" ]; then
echo >/dev/stderr
exec /usr/bin/sudo "$@"
else
exec echo -e "\n:-)" >/dev/stderr
fi
That way even if I already typed my password, I systematically get asked if I'm sure I want to execute the command.I think I'm pretty proficient as a basher, but I also never use the bang, nor do I use the 'history' bit. Didn't know about alt-. tho, that's handy.
I could toss 'control-k' for 'delete to end of line' that hasn't been mentioned. And of course control-P/N for previous/next commands (or arrows obviously).
Neither do I, intentionally at least.
This week I encountered a very bizarre problem when running GHCi (a Haskell REPL), invoked from bash in an Emacs "shell" buffer.
In Haskell, "!!" is a function for getting an element from a list, e.g.
> let myList = ["a", "b", "c"]
> (myList !! 1, myList !! 2)
This would normally return the pair ("b", "c"), which GHCi prints to the screen. However, for some reason it was performing bash-like subtitution, giving me (on subsequent retries): parse error on input 'let'
(myList (myList !! 1, myList !! 2) 1, myList (myList !! 1, myList !! 2) 2)
(myList (myList (myList !! 1, myList !! 2) 1, myList (myList !! 1, myList !! 2) 2) 1, myList (myList (myList !! 1, myList !! 2) 1, myList (myList !! 1, myList !! 2) 2) 2)
and so on exponentially!On a similar note, I always get nervous when using "rm -rf", since the path is written in big-endian order, so an accidental nudge halfway through typing can cause something like
rm -rf ~/Pictures/Scans/NeedConverting/*
To become rm -rf ~rm -rfi
lets you get an interactive mode so you can confirm what's happening before it gets erased.
The class of shortcuts I love is:
[command] !:1-$
which copies the arguments to the previous command to this one. Or:
!:3
which copies the fourth argument.
Once those were under my fingers my fellow architects were astounded.
shopt -s histverify
versus
shopt -u histverify
and then add whatever you prefer (-s probably) to your bashrc
Alt-. will generally add the prior last argument into the current command, and repeated presses will cycle through previous commands.
Ctrl-r will dynamically update to show you the prior command matched as you start typing afterwards, so you can confirm it's correct.
Both are essential if you want to reuse prior commands but either can't trust that you were the last person using the shell, or don't want a typo to ruin your day, and possibly many other people's day as well. That is, if you're a sysadmin using the automatic variants, I don't want you anywhere near systems I rely on, I want you far away. Perhaps working for a competitor.