How to Repeat a Previous Command at the Command Line on Linux

Linux, as well as other Unix-based operating systems like OS X and FreeBSD, uses the concept of a root super user to keep unwanted execution of code to a minimum. The sudo (super user do) command is necessary to execute commands requiring such access when a user is currently working under a standard user shell and doesn’t currently have these privileges. This must be typed before the command and then an account password has to be entered if it hasn’t been run previously in the same shell session within a certain amount of time.

Sometimes you may not remember that you needed to use sudo until after you’ve already issued the command and are now privy to an error message. You certainly won’t want to have to loose your work merely because you neglected to issue the sudo modified. Fortunately a quick trick will prevent this from happening. You can run a previous command as the super user with it. This same trick can then also be used without super user privileges to simply repeat a previous command you gave.

Method 1: Run a Previous Command as the Super User

Assume you were already inside of a CLI that could be in a terminal emulator window or in a virtual console and then assume you were typing away a command only to realize that you needed to use sudo in order to get it to run. Once you’re returned to the prompt after the error message, type sudo !! and then approve your password. This will run the same exact command. In the example given in the screenshots here, the user attempted to run the unhide security program, but received an error message. They then typed sudo !! and pushed return followed by the password in order to run the program as though nothing had happened. This can be used with any command that you neglected to use sudo with.


Method 2: Run a Previous Command as a Standard User

If you simply wish to run the previous command a second time, then merely type two exclamation points at the CLI prompt and then press return. The Bourne Again Shell will then echo the code of the previous command before promptly running it a second time.

Kevin Arrows

Kevin is a dynamic and self-motivated information technology professional, with a Thorough knowledge of all facets pertaining to network infrastructure design, implementation and administration. Superior record of delivering simultaneous large-scale mission critical projects on time and under budget.

Expert Tip

How to Repeat a Previous Command at the Command Line on Linux

If the issue is with your Computer or a Laptop you should try using Restoro which can scan the repositories and replace corrupt and missing files. This works in most cases, where the issue is originated due to a system corruption. You can download Restoro by clicking the Download button below.

Download Now

I'm not interested