Bash history

Bash will remember all command you enter so it is possible to repeat them without retyping. See also Bash Event Designators

With arrow up or arrow you can cycle through all the commands.

List all commands entered so far

history
2024 ls /tmp
2025 date
2026 grep error /var/log/syslog

You can use those numbers to directly repeat such a command with. E.g. this will repeat the date command in my example

You can also use -x to refere to the command x before us. E.g. -1 is the last command, -2 the one before that ...

This will repeat the very last command in the list (in this case the grep command)

Warning the command is executed immediately, so no need to press return.

Repeat last command that started with gi

Something very similar is CTRL+r to search for previous command. For example type this to also search for the last command starting with gi

CTRL r gi

You can press CTRL + r again to search for even older commands on your history that also start like this or continue typing to restrict the search to more exact hits. Maybe while search backwards you reconsinder and want to back to younger hits. No problem, with CTRL + s you can search in the other way again. Only drawback is, that CTRL + s may be bound to another function. You may try this to get it back working

stty -ixon

Add it to your .bashrc to have it always working.

Very often you need to reuse parameters of previous commands. For example, you copy one file to another and want to modify the new file. Normally you would do

cp template.txt newlist.txt
vi newlist.txt

Instead of re typing the last parameter you can use !$ to use the last word of the previous command

cp template.txt newlist.txt
vi !$
# vi newlist.txt

If you need not only the last parameter but all parameters

vi a.txt b.txt c.txt
tar -cf backup.tar !*
# tar -cf backup.tar a.txt b.txt c.txt

You may want to repeat a command but change something in it. Just type ^old^replacement to achieve this. For example if you tried to edit files with vi but want to use emacs instead

vi a.txt b.txt c.txt
^vi^emacs
# emacs a.txt b.txt c.txt

Command line help

You will need to repeat parameters very often, e.g. when you create copies. Some examples of this:

cp document_old.txt document_new.txt
cp document.txt     document_Backup.txt

With the {first,second} syntax you can have that much shorter

cp document_{old,new}.txt
cp document{,_Backup}.txt
ShortcutDescription
Ctrl+Amove to the start of the line
Ctrl+Emove to the end of the line
Ctrl+UCut from the cursor to the beginning of the line into the clipboard
Ctrl+KCut from the cursor to the end of the line into the clipboard
Ctrl+WCut from the cursor to the start of the word into the clipboard
Ctrl+Ypastes text from the clipboard

Bash configuration

export HISTSIZE=1000Remember the last 1000 commands in memory for one session
export HISTFILESIZE=2000Persist the last 2000 commands on disk to load it next time
export HISTCONTROL=ignorebothDon't store commands which start with a space or successive identical entered commands
shopt -s histappendThe last Bash session you close won't overwrite the history
shopt -s histverifyCommands you repeat with ! are not executed immediately