Command Line history.

      history [n]

      history -c

      history -d offset

      history -anrw [filename]

      history -p arg [arg ...]

      history -s arg [arg ...]

   -c        Clear the history list by deleting all the entries.
-d offset Delete the history entry at position offset. -a Append the 'new' history lines (history lines entered since the beginning of the current bash session) to the history file. -n Read the history lines not already read from the history file into the current history list. These are lines appended to the history file since the beginning of the current bash session. -r Read the contents of the history file and use them as the current history. -w Write the current history to the history file, overwriting the history file’s contents. -p Perform history substitution on the following args and display the result on the standard output. Does not store the results in the history list. Each arg must be quoted to disable normal history expansion. -s Store the args in the history list as a single entry. The last command in the history list is removed before the args are added.

With no options, display the command history list with line numbers. Lines listed with a * have been modified.

An argument of n lists only the last n lines.

Saving History, after executing a series of commands save them all with:
$ history > cmds.txt

If filename is supplied, it is used as the name of the history file; if not, the value of HISTFILE is used.


HISTCONTROL controls how bash stores command history. The ignorespace flag will ignore commands that start with spaces (prefix with a space any commands that you don't added to history), ignoredups, will ignore duplicates and ignoreboth will ignore both spaces and duplicates. equivalent to: ignorespace:ignoredups.

export HISTCONTROL=erasedups

HISTFILE defines the history file, by default ~/.bash_history

export HISTFILE=~/.history2

HISTSIZE defines the number of lines of history to save in memory (by default 500). The history file is truncated, if necessary, by removing the oldest entries, to contain no more than that number of lines
export HISTSIZE=5000

HISTFILESIZE defines the number of lines of history to save in the history file (~/.bash_history) when the shell exits (by default 500).
export HISTFILESIZE=5000

If the shell variable HISTTIMEFORMAT is set and not null, it is used as a strftime format string to display the time stamp associated with each displayed history entry. No intervening blank is printed between the formatted time stamp and the history line. In addition, the time stamp information associated with each history entry will be written to the history file.

The return value is 0 unless an invalid option is encountered, an error occurs while reading or writing the history file, an invalid offset is supplied as an argument to -d, or the history expansion supplied as an argument to -p fails.

This is a BASH shell builtin, to display your local syntax from the bash prompt type: help history

Various shells have options that can affect this. Be careful with shells that let you share history among instances. Some shells also
allow bang commands to be expanded with tabs or expanded and reloaded on the command line for further editing when you press return.

“In times of change, learners inherit the earth, while the learned find themselves equipped to deal with a world that no longer exists” ~ Eric Hoffer

Related macOS commands

shopt -s histappend - Append history to file (~/.bash_history) by default history is not saved.
complete - Edit a command completion [word/pattern/list]
fc - Fix command (history)
bind - Set or display readline key bindings to recall history.
Startup profile - Set history commands at every login.

Copyright © 1999-2023 SS64.com
Some rights reserved