Ways to Re-run Last Executed Commands in Linux - GeeksforGeeks

30 Jun, 2021
It would be quite efficient to re-execute the last command. If the command was quite big to type in again, this would be quite a great trick to improve efficiency. So let us see what the commands are to run the last executed command again in the Linux terminal. The ways to run the last executed command are as follows:
Ctrl + PUp Arrow key.bang-bang operator. Nth bang operator.fc command.
Ctrl + P
Up Arrow key.
bang-bang operator.
 Nth bang operator.
fc command.
This is the most dependable shortcut in Linux to execute the last run command in the terminal. Just press the Ctrl and P keys together to fill the prompt with the last executed command and you are ready to go. This method works in bash perfectly even after closing the terminal, but it might not work in zsh after closing the session. But still, it is quite more reliable than other methods.
Gif to demonstrate the usage of CTRL+ P
The above GIF demonstrates the usage of CTRL + P  to run the last executed command in Linux. In that example, the cat command was used before and CTRL + P was used to run it again. If the command was quite big, then CTRL + P becomes very useful.
Simply works most of the time, using arrow keys is the most popular way to search through previous commands. This is the most known and used a keyboard shortcut to run the previous commands. The usage It’s quite straightforward to use the up arrow key to search through the previously executed command.
Demonstrating usage of upArrow to re-run the last executed command.
Double bang or !! is also one of the ways where you can run the last command. It is also known as the bang-bang command, which repeats the previously executed command as previously mentioned.
!!
Using bang bang to re-run the last executed command.
The above screencast is a demonstration of using the bang-bang operator to run the last executed command. This doesn’t give a clear command to the user if he/she doesn’t have the idea of the previously executed command.  You cannot even edit the command before executing it, so choose this only if you are sure.
The single band operator is followed by the number, which tells it which previous command it would run. For running the last command, we use !-1, indicating the last command, if we use !-2, we will run the second-last command and so forth. The bang operator searches for commands from the shell history and executes as the mentioned number accordingly.
!-1 
Using a bang operator to re-run the last executed command.
In this command, you cannot make changes like previous commands like CTRL + P and up arrow key, you cannot even view the command before pressing enter, just like the bang-bang command. Use this only if you are confident about the last executed command.
Though we also have a bit of freedom to choose from if we use ! before any command such as !source, it will run the last executed command with the first match only and not in the embedded command.
FC is a program built-in certain popular shells to work with the previously executed commands. We can use it for executing the last command very easily by just entering a flag or argument into it.
fc -s
By adding in -s, we can easily execute the last run command. 
Using fc -s to re-run the last executed command.
Quite similar to the bang commands, it doesn’t show the command we might be executing. So we have to be sure about the command we are looking for. The above screencast shows the usage of fc with -s to re-run the last executed commands. 
linux-command
Picked
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
scp command in Linux with Examples
Docker - COPY Instruction
mv command in Linux with examples
SED command in Linux | Set 2
chown command in Linux with Examples
nohup Command in Linux with Examples
Named Pipe or FIFO with example C program
Thread functions in C/C++
uniq Command in LINUX with examples
Start/Stop/Restart Services Using Systemctl in Linux