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
Important Points
- show the limited number of commands that executed as follows:
history 5
Note:
The command can also be executed using event number .
Example:
!231
2. you can also print command before executing it by using :p after event number of command.
Example:
!231:p
3. to execute the 6th command from the History list Bottom :
!-6
4.This Command can also be used along with grep:
Example:
history | grep yum
5. The most recent command can be viewed using !!
Example:
!!
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:
7. The command can also be executed using a part of the command.
Example:
!command_starting_string
8. History can also be removed using history -d event_number.
Example:
history -d 566
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
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