5 Ways To Repeat Your Last Command In Linux
Howdy! Hope you’re enjoying the weekend. Today, I will be teaching you how to repeat your last command in Linux. That means you don’t have to re-type the last executed command in your Shell. Excited? Follow me. You can, of course, use the up arrow key to select the last executed commands and hit ENTER to execute them. However, there are also other few ways to do this. There could be many ways to repeat the last commands in Unix-like systems, but these are the only ways I know now. All these commands were tested in my Arch Linux with BASH shell.
A note of caution: When you try the following methods, the previously executed commands will run immediately. Just make sure your last command wasn’t any harmful like formatting or deleting a drive, file or any important data.
Repeat Your Last Command In Linux
Let us run some commands first.
As you all might already know, we can execute the last command by simply pressing the up arrow and hit ENTER key. This is the most common way used by many users to execute the previous command. This method will work on all SHELL, regardless of the Linux operating system you use.
However, like I said already, there are few other methods to do this.
Now, let me show you how to execute the last command with some practical examples.
To execute any last executed command, just type double exclamation marks, and hit ENTER:
This will execute the last command. Sample output would be:
uname -r 4.10.13-1-ARCH
Add “sudo” in-front of “!!” to execute the last command as root user like below.
Also, this command will execute the previous command:
su -c "!!"
su -c "uname -r" Password: 4.10.13-1-ARCH
Cool, isn’t it? You don’t need to type the last command completely. It could be useful when you’ve already executed a very long command, and don’t want to re-type the same command fully.
You might have run so many commands, and want to repeat a specific command. What will you do? Simple! You can do this by typing a specific word of the previous command.
To repeat previous command that starts with a specific word/letter, for example un, just type:
The above command will execute last command that starts with the letters “un”.
Sample output for the above command would be:
!un uname -r 4.10.13-1-ARCH
As you see in the above example, you don’t need to type the whole command (i.e uname -r). Instead, just type few letters of a command, and any previous command that contains words will run.
Also, If you know the full command name, just type it like below:
It will execute the last command.
Another way to do this is by searching your command line history using CTRL+R. Press CTRL+R key to search through the command line history. I personally prefer this method. It searches history interactively which I think feels safer than executing blindly from it.
Look at the following example. In the following example, I searched for “un”, which displayed the last command “uname -r” in the history that contained the word “un”. Then, I hit ENTER to execute it.
(reverse-i-search)`un': uname -r
Just in case, you want to edit/modify the last command before executing it, just press the left arrow key, then edit the last command and hit ENTER to execute it.
Here is yet another way to run the previous command.
uname -r 4.10.13-1-ARCH
Similarly, !-2 will run the second last command, !-3 will run the third last command, and so on.
I don’t want to type any command manually. Is there any way to repeat a last command by simply pressing a particular key(s) in my Keyboard? Yes, of course!
Press CTRL+P to switch to the last command, and then press CTRL+O to execute it. This will do the wonder. No configuration needed! You can use CTRL+O as many times as you want to keep re-executing the last commands.
And, that’s all. You know now how to repeat your last command without typing it in the Terminal. If you want to view the output of your last command without having to retype the last command, these methods will help. Have fun!
If you find our guides useful, please share them on your social, professional networks, so everyone will benefit. I will be back with another interesting guide soon. Till then, stay safe!
Thanks for stopping by!
Help us to help you:
- Subscribe to our Email Newsletter : Sign Up Now
- Support OSTechNix : Donate Via PayPal
- Download free E-Books and Videos : OSTechNix on TradePub
- Connect with us: Facebook | Twitter | Google Plus | LinkedIn | RSS feeds
Have a Good day!!