Let me tell you how to maximize your productivity on the Bourne Again SHell while minimizing your effort. bash has a ton of tricks and shortcuts that allow you to command it with little effort, and I intend to show you the features that help me day in and day out.
Today I’m going to explain the use of features like history, brace and file expansion, and other tricks by example and give you references for later.
Master your history
Those who forget history are doomed to repeat it. This is arguably one of the best productivity enhancing features of any shell.
You can check your history with the
history command, which prints your last 500 commands (or so) by default. Alternatively, you can filter the list:
Each entry has a number, which you can then execute with
Now suppose I want to copy a file to a directory and then change to that directory. The quick way to do that with history is:
or if I forget to run a command as super-user:
to execute the last command starting with “mount”, since I don’t want to type it all out:
Note that I often (but not always) prefer
Ctrl-R, which will search history as you type. As an added bonus you can view the command before executing it.
expands to the last command and all arguments
3rd-to-last command and all arguments
first argument of the last command in history
2nd argument of the last command
last argument of the last command
all arguments of the last command, but not the command itself
expands to the 42nd command in the history list
last command beginning with “foo”
last command containing “baz”
last command with the first occurrence of “foo” replaced with “bar”
last command with all occurrences of “foo” replaced with “bar”
prints command without executing
Helpful .bashrc entries for history
Copy and paste these into
Another neat trick with .inputrc
If you are still particularly fond of the up and down arrows, copy and paste the following into a
~/.inputrc file. This will allow you to start typing a command and then hit the up-arrow to search backwards through your history for commands starting with what you typed. I prefer other methods usually but this is pretty cool, huh?
Peteris Krumins has an excellent write-up called The Definitive Guide to Bash Command Line History which goes in-depth on many of the above topics, should you crave more bash history goodness.
No shorthand list would be complete without the (in)famous brace expansions. Basically, they allow you to specify part(s) of an command to repeat substituting different values of a set within braces. Let me show you what I mean:
This is obviously very useful to prevent having to repeat parts of files or directory paths.
Suppose I wanted to make a template folder structure, I could make most of the directories I need with:
This will expand every combination so I end up with src/com/eriwen/data, src/com/eriwen/view, test/com/eriwen/data, and so forth. Being able to create a template directory structure with one line is a big time saver!
Better filename expansion
I’m sure you often use the * operator to match files beginning or ending with something, but bash goes far beyond that. It should be noted, though, at some point a good
find | grep is more powerful and useful. See Find is a beautiful tool for more information.
In addition to wildcards with
*, you can use
? to match any single character. You can also limit matching to certain characters with
. For example:
.bashrc entries for better filename expansion
There are a couple quick tricks to change to oft-used directories. For example:
You can also switch to the previous directory with
cd - like so:
To get more advanced with this, try mastering your directory stack with pushd and popd.
Effective use of history, brace expansions, and other shortcuts will to save you a lot of time. However, nothing is more productive than no typing. Automating things is best where possible. If you can’t automate, use smart aliases.
I know I did not cover tilde expansions, shell parameter expansions or bash key commands. You will want to check those out as well, but I find them less useful than what I’ve covered here.
Have any quick shortcuts you love? Share them in the comments!