Linux Shell History Introduction

‘history’ command will display a list of last used commands in the shell on your system.

Sample syntax of :

$ history <number>  

Like example of :

$ history 10  

See your terminal after running the command, we have used history -10. You can use any integer other than 10.

Here in the example, it displays list of last ten commands.

To clear your history use

$ history -c

history -c” command will clear your entire command history. After clearing running a history <n> will return an empty result.

Ctrl + r

‘Ctrl+r’ searches the commands from the history matching the specified characters that you type on your terminal.

Press Ctrl+r and then type any characters, the most recent match will be automatically shown in the terminal. The closest match is from the closest time interval from now. Note that, this won’t list entire results that matches the query, but will show only one command which matches first.

Like example of :

press 'ctrl+r'. ca  

See your terminal after run the command, we have pressed ‘ctrl + r’ followed by two characters ca and it has found the last command matching characters ‘ca’. It is very helpful.

Linux Shell History Commands

Interacting with the terminal, you will be typing some commands very often and sometime typing with variations on those commands.

Sometimes it can be very irritating as well as a little bit time consuming to type same large commands over and over.

Bash shell provides some commands to repeat the history of the commands used. The shell makes it easy for us to repeat the commands we have used.

Shell History Search Command

!!

Repeats last typed command.

!

Repeat other commands other than the last typed command.

$ history 

Shows a list of older commands. Notice the first integer. This is the line number. If we use that number as !n, the bash repeats that command.

!n

Repeats the command at line number n. Then number n is the line number defined by the ‘history’ command.

Ctrl + r

Searches for a command in history, and executes it, when pressed enter.

Environment Variables which controls the history

$HISTSIZE

Determines number of commands stored in the history.

$HISTFILE

Displays location of file that contain history.

$HISTFILESIZE

To set number of commands stored in history file

To view the contents of an environment variable use echo.

Example:

$ echo $HISTSIZE

Keys In History Command:

UP Arrow KeyIt will scroll you backward in history.
Ctrl + pIt will scroll you backward in history.
DOWN Arrow KeyIt will scroll you downwards in history.
Ctrl + nIt will scroll you downwards in history.
Ctrl + rIt will take you to the command matching the specified character.
Ctrl + gIt means escape from history searching mode.

Leave a Reply