History Command is used to view the previously executed command and it can also list it.
This feature was not available in the Bourne shell.
Bash and Korn support this feature in which every command executed is treated as the event and is associated with an event number using which they can also be recalled and changed if required.These commands are saved in a history file. in Bash shell history command shows the whole list of the command.
Syntax:
history
![](http://unixcop.com/wp-content/uploads/2021/05/2021-05-15-18_03_20-Greenshot-1.png)
Important Points
- show the limited number of commands that executed as follows:
history 5
![](http://unixcop.com/wp-content/uploads/2021/05/2021-05-15-18_05_28-Greenshot.png)
Note:
The command can also be executed using event number .
Example:
!231
![](http://unixcop.com/wp-content/uploads/2021/05/2021-05-15-18_07_53-Greenshot.png)
2. you can also print command before executing it by using :p after event number of command.
Example:
!231:p
![](http://unixcop.com/wp-content/uploads/2021/05/2021-05-15-18_10_17-Greenshot.png)
3. to execute the 6th command from the History list Bottom :
!-6
![](http://unixcop.com/wp-content/uploads/2021/05/2021-05-15-18_13_15-Greenshot.png)
4.This Command can also be used along with grep:
Example:
history | grep yum
![](http://unixcop.com/wp-content/uploads/2021/05/2021-05-15-18_15_59-Greenshot.png)
5. The most recent command can be viewed using !!
Example:
!!
![](http://unixcop.com/wp-content/uploads/2021/05/2021-05-15-18_18_25-Greenshot.png)
6. So suppose if the command has to be executed without being stored variable HISTFILE has to be unset.
So the command will be:
Example:
![](http://unixcop.com/wp-content/uploads/2021/05/2021-05-15-18_20_14-Greenshot.png)
7. The command can also be executed using a part of the command.
Example:
!command_starting_string
![](http://unixcop.com/wp-content/uploads/2021/05/2021-05-15-18_23_42-Greenshot.png)
8. History can also be removed using history -d event_number.
Example:
history -d 566
![](http://unixcop.com/wp-content/uploads/2021/05/2021-05-15-18_26_45-Greenshot.png)
NOTE:
The “.bash_history” hidden file stores every command you wrote, every user has a bash history even the super user ‘root’.
9. the whole history can be removed using history -c option but it can’t be removed from the “.bash_history“.
Example:
history -c
![](http://unixcop.com/wp-content/uploads/2021/05/2021-05-15-18_28_12-Greenshot.png)
10. So to remove the whole history from the “.bash_history” file you can use this :
echo " " > .bash_history
11. You can also know your history list size :
echo $HISTSIZE
![](http://unixcop.com/wp-content/uploads/2021/05/2021-05-15-18_29_27-Greenshot.png)