Overview
The history command displays a list of previously executed commands in the current Nash shell session. Each command is shown with a line number for easy reference.
Syntax
Description
The history command shows commands that have been executed in the current interactive Nash session. Commands are numbered sequentially starting from 1. An optional argument can limit the output to the last N entries.
Arguments
Limit output to the last N history entries. If not specified, all history entries are displayed.
Examples
Display All History
history
# Output:
# 1 ls
# 2 cd /home/user
# 3 cat file.txt
# 4 grep pattern data.txt
# 5 history
Display Last N Commands
history 3
# Output:
# 3 cat file.txt
# 4 grep pattern data.txt
# 5 history 3
Display Last 10 Commands
history 10
# Shows the last 10 commands executed
Display Single Last Command
history 1
# Output:
# 5 history 1
# (shows only the most recent command)
Each line follows this format:
Line numbers are right-aligned and padded with spaces
Line numbers are at least 5 characters wide
Two spaces separate the line number from the command
Commands are shown exactly as they were entered
Practical Use Cases
Review Recent Commands
history 20
# Review the last 20 commands to recall what you've done
Find Previously Used Commands
history | grep git
# Find all git commands in history
Search for Specific Pattern
history | grep -i api
# Find all commands containing "api" (case-insensitive)
Count Total Commands
history | wc -l
# Output: 42
# Shows total number of commands in history
Extract Command by Number
history | grep "^ 5"
# Find command number 5
Recent Command Summary
echo "Last 5 commands:"
history 5
Audit Session Activity
# Save history to file for review
history > session-log.txt
echo "Session saved to session-log.txt"
Filter by Command Type
# Find all file operations
history | grep -E "cp|mv|rm|touch"
# Find all text processing
history | grep -E "grep|sed|awk|cut"
# Find all directory operations
history | grep -E "cd|mkdir|ls|pwd"
Check Command Frequency
# Find most commonly used commands
history | cut -c8- | cut -d ' ' -f1 | sort | uniq -c | sort -rn
Review Long Commands
# Find commands longer than 50 characters
history | grep -E ".{50,}"
Combining with Other Commands
Filter and display
Count specific commands
Last command only
Extract command text
history | grep export
# Output:
# 7 export API_KEY=abc123
# 12 export PORT=8080
History Persistence
Nash command history is session-based and not persistent :
History is stored in memory only
History is cleared when the shell exits
Each new Nash session starts with empty history
No history file is saved to disk by default
If you need to preserve history between sessions, manually save it: history > ~/.nash_history
Interactive Session Only
The history command only works in interactive mode: # Interactive shell (works)
nash
user@nash$ history
# Shows history
# Non-interactive (empty)
nash -c "history"
# Shows nothing (no interactive history)
# Script file (empty)
echo "history" > script.sh
nash script.sh
# Shows nothing (no interactive history)
History Size
Nash maintains unlimited history during a session (memory permitting). There is no built-in limit on the number of commands stored.
# Check history size
history | wc -l
# Returns total number of commands in current session
Command Recall
While Nash stores command history, it currently does not support:
History expansion (!N, !!, !string)
Reverse history search (Ctrl+R)
History substitution
History is view-only via the history command.
Limiting Output Examples
# Last 5 commands
history 5
# Last 10 commands
history 10
# Last 100 commands
history 100
# All commands (no limit)
history
Notes
History starts at line number 1
Each executed command is immediately added to history
The history command itself appears in history
Line numbers are sequential and don’t skip
Empty commands are not added to history
Invalid limit values are ignored (shows all history)
Common Patterns
Session Documentation
# At end of session, save history with timestamp
echo "Session $( date )" > session-notes.txt
history >> session-notes.txt
Recent Activity Check
echo "Recent activity:"
history 10
Command Statistics
echo "Total commands: $( history | wc -l )"
echo "Last command: $( history 1 | cut -c8- )"
Find Command with Pattern
# Find all export commands
history | grep "^.*export"
Exit Status
The history command always returns exit code 0 (success).
help - Display help information
which - Check command availability
grep - Filter history output
cat - View saved history files