Press "Enter" to skip to content

How To Run Previous Commands In Linux/Unix Shell

A very nice and time saving feature of the Bash shell is that it has many ways for editing and running previous commands in Linux and Unix, but remember sometimes it does not work automatically and then you have to give the command bash in command prompt to let shell come in that mode. The following are some methods by which you can execute previous command in Linux/Unix quickly.

1. Scrolling with the up and down arrow keys
2. Using Ctrl+P and Ctrl+N
3. Listing command history

Scrolling with the Up and Down Arrow Keys

You can use the up and down arrow key to scroll though your previously executed commands in bash shell.

Pressing Ctrl+P and Ctrl+N

Use Ctrl+P to display your previously entered command and use Ctrl+N to scroll back command stack.

Listing Command History

Use the history command to display commands that have been previously entered, as shown in below example:

$ history 3

It will show you previously entered 3 commands, below is the example:

125 ls *.ctl
126 cp abx.txt usrabc
127 history 3

To run any command from above list, type an exclamation mark followed by the history number eg. to run the first command of the list which is having 125 number, do the following:

$ ! 125
To run the last command you can use !!, as following:
$ !!

Search command history in linux/unix shell

Vinish Kapoor

FoxInfotech.in is created, written, and maintained by Vinish Kapoor. It is built on WordPress, and hosted by Bluehost. Connect with Vinish on Facebook, Twitter, and GitHub.

You may also like: