

Different Ways To Repeat Your Last Command In Linux - OSTechNix
source link: https://www.ostechnix.com/5-ways-repeat-last-command-linux/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Different Ways To Repeat Your Last Command In Linux
Today, I will be teaching you how to repeat your last command in Linux. You don't have to re-type the previously executed commands in your Shell. Of course, we use the UP arrow key to select the last executed commands and hit ENTER to execute them. However, there are also other a 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 am aware of now. All these commands were tested in my Arch Linux and Ubuntu 18.04 LTS desktop with BASH shell.
A word 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 a partition or deleting files, folders or any other important data.
How To Repeat Your Last Command In Linux
Let us run some commands first.
$ ls -l
$ pwd
$ date
$ uname -r
As I mentioned earlier, 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 distribution you use.
However, like I said already, there are a few other methods to do this.
Now, let me show you how to execute the last command with some practical examples.
Method 1 - Using exclamation marks:
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 5.3.0-40-generic
Add "sudo" in-front of "!!" to execute the last command as root user like below.
$ sudo !!
This command will also execute the previous command:
$ su -c "!!"
Sample output:
su -c "uname -r" Password: 5.3.0-40-generic
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.
Method 2 - Repeat N-th command:
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:
$ !un
The above command will execute last command that starts with the letters "un".
Sample output for the above command would be:
!un uname -r 5.3.0-40-generic
As you see in the above example, you don't need to type the whole command (i.e uname -r). Instead, just type a few letters of the command, and any previous command that contains words will run.
Also, If you know the full command name, just type it like below:
$ !uname
It will execute the last command.
What if you don't want to repeat the last command, but just display it. Sometimes, you don't to run a command, but just retrieve it from the history. If so, find the prefix number of the command you want to run:
$ history | grep -i "source"
Sample output:
1664 source googler_at 1678 source googler_at 1685 source ~/.bashrc 2037 history | grep -i "source"
Let us say you want to retrieve the 1685th command, but don't want to run it immediately, add :p next to the command like below.
$ !1685:p
This will display 1685th command from the history, but won’t execute it.
Method 3 - Using reverse-search:
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 commands blindly from the BASH history.
Have a look at the following example. In the following example, I searched for "ost", which displayed the last command “sudo netctl restart ostechnixjio” in the history that contained the word "ost". Then, I hit ENTER to execute the command immediately or right arrow key complete the command and hit ENTER key to execute it.
(reverse-i-search)`ost': sudo netctl restart ostechnixjio
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. Some commands may start with same letters. In such cases, keep hitting the CTRL+R key to bring back the command you wanted to run.
Method 4 - Use hyphen symbol with command prefix numbers:
Here is yet another way to run the previous command.
$ !-1
Sample output:
uname -r 5.3.0-40-generic
Similarly, !-2 will run the second last command, !-3 will run the third last command, and so on.
Method 5 - Using CTRL+p and CTRL+o:
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.
Method 6 - Using 'fc' cmmand:
This is another way to repeat the last executed command. The 'fc' command is used to list, edit and re-run the previously entered command.
To repeat the last command using fc, simply run:
$ fc -s
Sample output:
uname -r 5.3.0-40-generic
Suggested read:
Method 7 - Using ALT key
As one of our reader said in the comment section, we can also use ALT+. keys (press ALT and then press .) to retrieve the last command without running it. However, there is one limitation. Hitting ALT+. will only retrieve the last argument in the command. For example, if your previous command is "uname -r", it will bring back the -r argument, but not the entire command.
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 commands without actually having to retype them, these methods will help.
If you know any other methods, please let me know in the comment section below. I will check and update the guide accordingly.
You know now how to repeat a previously executed command. How do you repeat a Linux command or program until it succeeds? Well, that's easy! Refer the following guide for more details.
Hope this helps.
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: Reddit | Facebook | Twitter | LinkedIn | RSS feeds
Have a Good day!!
Recommend
-
146
Feel bored at work? Come on guys, let us play Piano! Yeah, you read it right. Who needs a real Piano? We can now play or learn how to play Piano from command line using our PC keyboard. Meet Piano-rs - a simple utility, written in...
-
124
Howdy Music lovers! Today I learned that we can control media players from commandline in Unix-like operating systems. You can play, pause, toggle between play and pause, increase/decrease volume, switch to next/previous media files, and stop the...
-
81
Nowadays, I spend more time in Terminal, trying to accomplish more in CLI than GUI. I learned many BASH tricks over time. And, here is the list of useful of BASH shortcuts that every Linux...
-
47
The festival season has started! Amazon and other popular online retail firms are readying their battle rooms with discounts, exclusive products and other attractions for shoppers. If you’re planning to shop on Amazon, I think...
-
112
Nowadays, I have learned to use CLI mode efficiently in my Linux desktop. Working from commandline is fun, fast, safe and recommended way to learn Linux. Everyday, I spend so much time on all Linux...
-
56
This guide explains how to find the mounted filesystem type in Linux operating systems.
-
23
The alt is a command line tool that helps you to switch between different versions of commands in Unix-like operating systems.
-
10
Vim's 'single-repeat' dot command Okay, so first things first, let me get this off my chest. Vim, I’ve been cheating on you. I know that I’ve said you’re all t...
-
9
Please – A Simple Command Line Todo Manager Manage Tasks And To-do Lists With 'Please' From Command Line In Linux Written by sk June 20, 2022...
-
7
How To Easily Recall Forgotten Linux Commands Using Apropos Written by sk Published: November 15, 2017
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK