history - macOS


The history command in macOS displays the list of commands that have been previously entered by the user in the current terminal session. This tool is invaluable for reviewing past commands, repeating commands without retyping them, and learning from past command usage. It is particularly useful in scripting, troubleshooting, and system management contexts.


The general syntax for the history command is:

history [options]

When used without options, history prints the list of past commands executed in the terminal session.


  • -c: Clear the history list. This option erases all the commands from the history of the current session.
  • -d offset: Delete the history entry at position offset. Offset starts at 1.
  • -s args: Add the args as a single entry at the end of the history list. Useful for scripting purposes.
  • -p args: Perform history substitution on the args but do not execute, just print the result. Useful for testing.
  • -a file: Append the new history lines (from this session) to a file, preserving existing content.
  • -r file: Read the history from file and append it to the history list of the current session.


  1. View all recent commands:
  2. Clear all command history:
    history -c
  3. Delete the fifth command from history:
    history -d 5
  4. Add a specific command to history manually:
    history -s ls -l
  5. Save recent session history to a file:
    history -a ~/my_command_history.txt

Common Issues

  • Duplicate commands: Repeated commands can clutter the history. Using export HISTCONTROL=ignoredups can help by not recording duplicate consecutive entries.
  • Commands not saving: If commands are not appearing in history, ensure HISTFILE is set correctly, and you have write permissions to that file.
  • Missing history after reopening terminal: This could be due to not saving the session history. Profile configuration and proper exiting of the terminal are necessary to save history.


Combine history with grep to search for specific commands:

history | grep "mkdir"

Use history in scripts to log or use previous operations dynamically. Here’s part of a script to reuse the last command:

last_command=$(history | tail -n 1 | sed 's/^[ ]*[0-9]*[ ]*//')
echo "Repeating last command: $last_command"
eval "$last_command"
  • !number: Re-run the command listed at number in history.
  • !!: Shortcut to re-run the last command.
  • !prefix: Re-runs the last command starting with prefix.
  • fc: A command that lists, edits, or re-executes commands from the history list.

For more extensive details on the usage and capabilities of history manipulation, the online manuals for bash or zsh can be helpful resources: Bash Manual, Zsh Manual.