The history command in Linux and Unix-like systems is a command line utility that displays a list of the previously executed commands in the current shell session. By default, the history command will display the commands in the order they were executed, with the most recent commands appearing at the bottom of the list. Each command is preceded by a number, which is the command's history number.
For example, if you run the history command in Linux, you might see output similar to this:
1 pwd 2 cd Mail 3 date 4 ls -la 5 history
This output shows the history of commands that have been executed in the current shell session. Each command is preceded by a number, which is the command's history number. You can use the history number to re-execute a command by prefixing it with an exclamation point (!) , for example !3 will execute the 3rd command in the history list, in this case it will be date
The output will be slightly different if you execute the history command in FreeBSD. The output will also show the time of execution for each command,
1 18:12 man history 2 18:12 w 3 18:13 ls -la 4 18:13 history 5 18:13 df 6 18:13 du
You can also use keywords or patterns to search for a specific command in the history list. For example, you can use the history | grep command to find all commands that contain a specific word.
history | grep "ls"
The history command also accepts options to customize the output, for example:
-c: Clear the history list. -w: Write the current history list to the history file. -n: Append the new history lines (those entered since the beginning of the current bash session) to the history file. -r: Read the history file and append its contents to the current history list. -p: Perform history substitution on the following args and display the result on the standard output. -a: Append the new_history list to the history file.
The history command is a powerful tool that allows you to easily recall and re-execute previous commands, and it's a great way to keep track of what you've done on the command line and to learn new commands.