About "sudo !!"

By R0bin | dev | 19 Feb 2022


Recently I saw in a video how someone used the command sudo !! to execute the previous command with root rights.
I've been using Linux for a long time, but irregularly, so I'm not familiar with all the tricks, and this one was definitely new to me. I already knew a few ways to save typing in the console, such as

  • ↑ [Up Arrow]
  • history | grep [insert search string here]
  • ⌃(Ctrl)-R

But sudo !! was something completely new. A small example will illustrate it:

Usage of

By the way, with sudo !5 you can re-execute the 5th command from the history list (I admire the person who has the number of a command executed a long time ago at the ready ;-) )

But what was that? The sudo man pages are silent about this parameter. man pages are usually very complete. This could only mean that !! is not an original option of sudo, but must have "deeper" roots in Linux. I did some research and quickly found what I was looking for.
! is the character of the feature "History Expansion", which can of course be found in the GNU Bash manual https://www.gnu.org/software/bash/manual/html_node/History-Interaction.html.

Have fun trying out sudo !!.

How do you rate this article?

1



dev
dev

Software development related stuff

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.