Ways to Re-run Last Executed Commands in Linux
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 + P
- Up Arrow key.
- bang-bang operator.
- Nth bang operator.
- fc command.
1) Ctrl + P
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.
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.
2) Up Arrow key
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.
3) Using a double bang(!) operator
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.
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.
4) Single bang (!) search
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.
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.
5) Using fc 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.
By adding in -s, we can easily execute the last run 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.