This tutorial explains which command is used to ‘re execute the previous command in Linux Ubuntu. Did you know that you can repeat previous command in Linux?
Let us learn more about re-executing previous commands.
Method 1 – Run Last Command using Exclamation Marks
To execute the last executed command, just type double exclamation marks, and press ENTER key in the terminal:
$ !!
This will execute the last command.
You can also run the command as root user. Simply add sudo in-front of !! to execute the last command as root user.
$ sudo !!
Or
$ su -c "!!"
Method 2 – Use Hyphen Symbol with Command Prefix Numbers
If you want to run a specific command which was executed lastly (but you don’t know when, was it the last command or the second last command or the third last command).
To execute the previous command, run the following:
$ !-1
Note that the number 1 denotes here the last executed command, similarly, !-2 will run the second last command, !-3 will run the third last, !-4 will run the fourth last command, and so on.
In this case you can use the history command to re-execute a certain command using hyphen symbol with the command prefix number.
When you run the history command you see the list of previously executed commands with their corresponding command number (prefix). Note down the prefix of the command and to re-execute the command with hyphen.
Method 3 – Re-execute Previous Commands Using CTRL+P
You can search the command line history using CTRL+P. Press CTRL+P key to search through the command line history and once you find the command you want to execute, press ENTER key.
This is also another way to repeat the last executed command. You can use the fc
command is used to list, edit and re-run the previously entered command.
And, that’s all. I hope you shall find this article useful and these methods will help yo in future.