Using bash history

From Hidden In Plain Sight
Jump to: navigation, search

History

List all the previously executed commands

$ history

Controlling history

history -c # Clear the history
HISTSIZE=0 # Control how many entries are recorded in the history
HISTCONTROL=ignorespace # Commands that start with a space are not recorded in the history
HISTCONTROL=ignoredups # Commands entered by using the up key are not recorded in the history
HISTCONTROL=ignoreboth # Both ignorespace and ignoredups

Executing commands from history

Execute a command from the history : !<command number in history>

$ !256 # Run command at location 256 in the history

Execute a command that was run previously : !-<number of commands previous>

$ !-5 # Run command typed five commands ago

Execute the last command : !!

$ !!

Search in history, press Ctrl-R and then start typing

Execute last command using keyword search : !<keyword>

$ !git # execute the last git command again

Arguments from commands in the history

Use the last argument from the last command : !$

$ echo !$ # Echo the last argument from the last command

Use the first argument from the last command : !^

$ echo !^ # Echo the first argument from the last command

Use a specific argument from a specific command : !<command search>:<argument number>

$ echo !git:2 # Echo the second argument from the last git command
$ echo !-1:5 # Echo the fifth argument from the last command