Quick Reference

suggest change

Interaction with the history

# List all previous commands

# Clear the history, useful if you entered a password by accident
history -c

Event designators

# Expands to line n of bash history

# Expands to last command

# Expands to last command starting with "text"

# Expands to last command containing "text"

# Expands to command n lines ago

# Expands to last command with first occurrence of "foo" replaced by "bar"

# Expands to the current command

Word designators

These are separated by : from the event designator they refer to. The colon can be omitted if the word designator doesn’t start with a number: !^ is the same as !:^.

# Expands to the first argument of the most recent command

# Expands to the last argument of the most recent command (short for !!:$)

# Expands to the third argument of the most recent command

# Expands to arguments x through y (inclusive) of the last command
# x and y can be numbers or the anchor characters ^ $

# Expands to all words of the last command except the 0th
# Equivalent to :^-$


These modify the preceding event or word designator.

# Replacement in the expansion using sed syntax
# Allows flags before the s and alternate separators
:s/foo/bar/ #substitutes bar for first occurrence of foo
:gs|foo|bar| #substitutes bar for all foo

# Remove leading path from last argument ("tail")

# Remove trailing path from last argument ("head")

# Remove file extension from last argument

If the Bash variable HISTCONTROL contains either ignorespace or ignoreboth (or, alternatively, HISTIGNORE contains the pattern *), you can prevent your commands from being stored in Bash history by prepending them with a space:

# This command won't be saved in the history

# This command will be saved

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents