Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Command History

Hash shell maintains persistent command history with bash compatibility.

Basic Usage

Up/Down Arrows

Navigate through history:

#> echo first
#> echo second
#> echo third

# Press Up arrow
#> echo third  # Most recent

# Press Up again
#> echo second

History Command

List all commands:

#> history
    0  ls -la
    1  cd /tmp
    2  echo hello
    3  git status

Multi-line Commands

A command typed over several lines is one history entry. A for loop, an if, a function or a here-document entered under the continuation prompt is recorded whole once it is complete, so Up brings the entire command back, spread over its lines, and Enter runs it again:

#> for i in 1 2; do
> echo "$i"
> done
1
2
# Press Up: the three lines come back together
#> for i in 1 2; do
echo "$i"
done

history lists such an entry on several lines, !n re-runs all of it, and Ctrl-C on an unfinished command records nothing.

History Expansion

!! - Last Command

#> echo hello
hello
#> !!
echo hello
hello

#> apt update
Permission denied
#> sudo !!
sudo apt update

!n - Command by Number

#> history
    0  ls -la
    1  cd /tmp
    2  echo test

#> !1
cd /tmp

!-n - Relative History

#> echo first
#> echo second
#> echo third
#> !-2
echo second
second

!prefix - Last Command Starting With

#> git status
#> ls -la
#> pwd
#> !git
git status

Environment Variables

HISTSIZE

Commands kept in memory:

export HISTSIZE=1000        # Default
export HISTSIZE=5000        # More
export HISTSIZE=-1          # Unlimited

HISTFILESIZE

Commands saved to file:

export HISTFILESIZE=2000    # Default
export HISTFILESIZE=-1      # Unlimited

HISTFILE

History file location:

# Default
~/.hash_history

# Custom
export HISTFILE=~/my_history

History File Format

The file holds one #<seconds> line before each entry, the same extended format bash uses with HISTTIMEFORMAT, so an entry may span several lines and the time it was entered is kept:

#1762459200
for i in 1 2; do
echo "$i"
done
#1762459207
echo done

A history file from an earlier release, with one command per line, still loads; it is rewritten in this format the next time a command is added.

HISTCONTROL

Control what gets saved:

export HISTCONTROL=ignorespace  # Skip space-prefixed
export HISTCONTROL=ignoredups   # Skip consecutive dups
export HISTCONTROL=ignoreboth   # Both (recommended)
export HISTCONTROL=erasedups    # Remove all dups

Privacy Feature

Commands starting with space are NOT saved:

#> echo public
# Saved to history

#>  echo private
# NOT saved (leading space)

What Gets Saved

Only the lines hash reads as its own input:

  • Commands typed at the prompt, whether they succeed or fail
  • Multi-line commands (&&, ||, ;)
  • Lines of a script run with hash-shell -i script

What Doesn’t Get Saved

  • Empty lines
  • Whitespace-only lines
  • Duplicate of previous command
  • Commands starting with space
  • Lines of files loaded with source or ., including ~/.hashrc and the startup files
  • Commands run through eval, or inside loop and function bodies
  • Anything after set -o nolog

History File

Location

Default: ~/.hash_history

Incremental Saving

History saves immediately after each command:

#> echo test1
# Immediately saved

#> echo test2
# Also saved

# Even if shell crashes, both are preserved!

Manual Commands

history -w    # Force save
history -r    # Reload from file
history -c    # Clear history (memory only)

Examples

Repeat Last Command

#> make
#> !!
make

Repeat with Sudo

#> apt update
Permission denied
#> sudo !!
sudo apt update
#> history | grep git
# See all git commands

Private Commands

#>  export API_KEY=secret
# Leading space - not saved!

Troubleshooting

History Not Saving

Check file permissions:

ls -la ~/.hash_history

!! Not Working

Make sure you’ve run a command first.

Up Arrow Not Working

Check terminal:

echo $TERM

Comparison with Bash

FeatureHashBash
!!YesYes
!nYesYes
!-nYesYes
!prefixYesYes
Up/Down arrowsYesYes
History fileYesYes
Privacy (space prefix)YesYes
Ctrl+RNoYes
!?patternNoYes
^old^newNoYes