3 Small Tips to Speed Up Your Bash Workflow
When I was in high school, we read a book called Cheaper by the Dozen. It's a semi-autobiographical novel about growing up in a family with 12 children. The father of the family was a man named Frank Bunker Gilbreth. Gilbreth made a career in consulting as an efficiency expert. He would study all of the motions that a person would make while doing their job, and look for ways to reduce the amount of motions necessary, in order to make the person more efficient at their job. He called these motions "therbligs," Gilbreth's last name spelled backwards except with the t and h swapped. For example he studied a surgeon performing an operation, and he noted the therbligs that a surgeon would make when they needed to get an instrument: Locate the scalpel, move the hand towards the scalpel, grasp the scalpel, move the hand back towards the operating site. He suggested to have an assistant to hand instruments to the surgeon, in order to make the surgeries more efficient.
Whenever I watch someone using the terminal, I usually notice a few digital therbligs people make that are unnecessary, and they cause mental speed bumps that slow down what they're trying to do. So here are three really small tips that you can easily commit to memory, that will help you speed up your Bash workflow and reduce mental speed bumps.
sudo !!
Did you forget to run the last command with sudo? Use sudo !!
. Bang bang, or !!
, repeats the last command where the two bangs are. You can also use !-[n]
where [n] is the number of commands back that you want to execute, and !!
is the same as !-1
There's a bunch more neat things you can do with bangs in your terminal, you can read more here, but sudo !!
is the easiest and most useful to add to your workflow.
ctrl + c
Most of us know that ctrl + c
kills the process running
in the foreground. But did you know, that if you want to get rid of what
you just wrote on the command line, you can also just press ctrl + c
?
Bash obviously just ignores the kill signal, but it also conveniently
clears the line. Far too often, I see people start typing a command out,
then they realize that they've typed the wrong command. Then they have
to remove their right hand from the center of the keyboard, move it over
to the backspace key, press it down, wait for the entire line to be
cleared, then bring the right hand back over to the center of the
keyboard. Instead, with one quick snap of ctrl + c
, the prompt is cleared, and you can go ahead and type what you intend to type.
Bash actually uses GNU Readline in order to read user input, and
Readline supports both Emacs and Vi editing modes (Emacs by default). So
you can also clear (and yank into the clipboard) everything before the
cursor with ctrl + u
. But ctrl + c
is much easier to remember because it's one of the first things we learn when starting out in the terminal. ctrl + u
can be useful though if you're in a program that will terminate if you press ctrl + c
, but you also want to clear the line.
ctrl + d
This command sends the End of File (EOF) character to Bash. When this happens, it's the equivalent of using the exit command in Bash. This is a handy way to close a terminal, close an ssh connection, or exit the Python interpreter very quickly.
I had another use for ctrl + d
a couple months ago as
well. I wanted to quickly test out some docker-compose files on a Docker
swarm, but I didn't want to bother copying them over. So I'd run the
following command:
docker stack deploy --compose-file - MyCoolStack
The dash there -
, is for reading from standard input. So I'd copy the whole file into my clipboard, paste it in, then hit ctrl + d
to tell Docker that the EOF has been reached.
Conclusion
I hope you found at least one of these tips helpful. If you want to learn more about Bash, you can read the documentation with the following command:
man bash