Bash Tricks
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
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
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
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
vi newlist.txt
Instead of re typing the last parameter you can use !$ to use the last word of the previous command
vi !$
# vi newlist.txt
If you need not only the last parameter but all parameters
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^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.txt document_Backup.txt
With the {first,second} syntax you can have that much shorter
cp document{,_Backup}.txt
Shortcut | Description |
---|---|
Ctrl+A | move to the start of the line |
Ctrl+E | move to the end of the line |
Ctrl+U | Cut from the cursor to the beginning of the line into the clipboard |
Ctrl+K | Cut from the cursor to the end of the line into the clipboard |
Ctrl+W | Cut from the cursor to the start of the word into the clipboard |
Ctrl+Y | pastes text from the clipboard |
Bash configuration
export HISTSIZE=1000 | Remember the last 1000 commands in memory for one session |
export HISTFILESIZE=2000 | Persist the last 2000 commands on disk to load it next time |
export HISTCONTROL=ignoreboth | Don't store commands which start with a space or successive identical entered commands |
shopt -s histappend | The last Bash session you close won't overwrite the history |
shopt -s histverify | Commands you repeat with ! are not executed immediately |
Jump to recently visited directories
Push a new directory to the stack and change to that directory
/tmp
# pushd /etc/network/
# pushd /var/log/
/var/log /etc/network /tmp
# pushd /usr/sbin/
/usr/sbin /var/log /etc/network /tmp
# dirs
/usr/sbin /var/log /etc/network /tmp
A simple pushd swaps the topmost directory on the stack with the next one and changes the current directory to the new topmost one. Very handy to jump between two directories.
/var/log /usr/sbin /etc/network /tmp
# pwd
/var/log
# pushd
/usr/sbin /var/log /etc/network /tmp
# pwd
/usr/sbin
With +x or -x you can select the x. directory (count starts with zero) counted from the left or the right side to be the new topmost directory.
/etc/network /tmp /usr/sbin /var/log
/# pwd
/etc/network
With popd the topmost directory is removed from the stack and the current directory will be set to the new topmost directory. With -n the current directory is not changed.
/etc/network /usr/sbin /var/log
# popd
/usr/sbin /var/log
# pwd
/usr/sbin
# popd
/var/log
# pwd
/var/log
With +x and -x you can again remove the x. directory counted from left or from right. If you remove the topmost directory (+0) the current directory is change to the new topmost directory.
You can also get the last directory you were before the current directory with the following variable
Command Substitution
Repeat last token of the last command
# echo !$
echo /dev/null
With
you get the last token at the current cursor position.
Repeat the last argument of the last command
# echo $_
/mnt/streamer/backup2008a.tar
Repeat the last command with a substitution
# ^2008^2009^
cp backup2009.tar /mnt/streamer/
However, this substitutes only the first occurrence, which also applies to the long version
# !!:s/2008/2009/
cp backup2009a.tar backup2008b.tar /mnt/streamer/
The long version can also substitute all occurrences
# !!:gs/2008/2009/
Turn Beep Off (e.g. while autocomplete)
setterm -blength
echo 'set bell-style visible' >> ~/.inputrc
$TERM
xterm
linux