Mastering Bash Bang (!
) Commands for Sysadmins
#
Ever forget to issue sudo
before a command? Ever had to repeat a whole command because of a typo? Wouldn’t it be nice to have shortcuts to repeat your previous commands? If this sounds like you, you’re in the right place! In this article, I’ll show you how to use bash bang (!
) commands to easily repeat commands and fix errors.
Command Repeat Basics #
Bash keeps track of the commands you’ve previously issued in your command history, controlled by the HISTSIZE
variable (default: 500). These commands are stored in the ~/.bash_history
file, with its location stored in the HISTFILE
variable.
Bang (!
) commands allow you to reintroduce commands from your history list into the input stream, helping you repeat commands, substitute text, manipulate arguments, and fix typos quickly.
Command Repeat Examples #
Repeat the Last Command Matching a String’s Beginning #
$ ls
dir dir1 dir2 file file1 file2 hello.txt
$ !l
ls
dir dir1 dir2 file file1 file2 hello.txt
Repeat the Last Command Matching Anywhere in a String #
$ cat hello.txt
Hello world ..!
$ !?hello.txt
cat hello.txt
Hello world ..!
Repeat the nth Command from Your History #
!10 # Runs the 10th command from the top of your history
Repeat the nth Command from Your Last History Entry #
!-10 # Runs the 10th command from the bottom of your history
Repeat the Last Command #
$ cat hello.txt
Hello world ..!
$ !!
cat hello.txt
Hello world ..!
Use Case: Prefixing sudo
or Piping Output
#
$ yum update
You need to be root to perform this command.
$ sudo !!
sudo yum update
Repeat with String Substitution #
Substitute a String in the Last Command #
$ ls /etc/httpd/conf.d
$ !!:s^conf.d^conf
ls /etc/httpd/conf
Substitute a String in a Specific Previous Command #
$ !l:s^conf^conf.d
ls /etc/httpd/conf.d
Alternative Substitution Syntax #
$ cd /etc/httpd/conf.d
$ ^conf.d^conf
cd /etc/httpd/conf
Repeat Command Arguments #
Repeat the nth Argument of the Last Command #
~/project $ ls -a -l
~/project $ ls !:1
ls -a
Repeat the Last Argument of the Previous Command #
$ mkdir dir3
$ cd !$
cd dir3
Repeat the Last Argument of a Specific Command #
$ mkdir -p hello/test1/test2
$ ls !mkdir:$
ls hello/test1/test2
Printing Commands Without Executing #
$ cat hello.txt
Hello world ..!
$ !:p
cat hello.txt
You can print any command in your history without executing it:
$ !mkdir:p
mkdir -p hello/test1/test2
$ !su:p
sudo yum check-update
Recall Commands with reverse-i-search
#
(reverse-i-search)`<search string>`: <output>
(reverse-i-search)`yu`: sudo yum check-update
(reverse-i-search)`cd`: cd /etc
- Press
CTRL + R
to activatereverse-i-search
. - Start typing to search your command history interactively.
Conclusion #
These bash bang (!
) commands are essential for sysadmins, saving time and providing quick ways to fix command errors. For more information, check the bash manual with:
man bash
By mastering these commands, you’ll enhance your productivity and streamline your terminal workflow. Happy bashing!