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

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

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

  • Successfully executed commands
  • Commands with errors
  • Multi-line commands (&&, ||, ;)

What Doesn’t Get Saved

  • Empty lines
  • Whitespace-only lines
  • Duplicate of previous command
  • Commands starting with space

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