Bash

How to Write Command Line Tools

The shell is a powerful tool that I think most people underestimate and under-utilize. Bash is probably the most common in the community, so we will refer to bash in all the examples, but all shells should support the same concept of redirection and piping. Below are some things to keep in mind when writing a program that is intended to run on the command line and play well with the shell.

Create Script from Bash History

The script program (bsdutils) will output the whole terminal output to a file. This can be helpful for logging and creating scripts out of history.

The approach here is to output the command history to a file and then manipulate the file a bit to get what we want.

history 3 > script.sh

This will output the last 3 commands to script.sh. Note that the actual history command will become the most recent event in history, and will be output in the file. This can be edited out manually or using this command:

Bash Shortcut Keys

Sometimes when working we don't always have a fully featured keyboard. Whether using a small portable bluetooth keyboard, old software, or you just want to be a command line ninja, these shortcuts are very important to know to increase productivity and to work through technical limitations on keyboards.

Useful Linux Bash Shell Aliases

Typing long commands over and over gets old. Use bash aliases to make your life easier and impress your peers. Add these to your .bashrc and/or .bash_profile. Personally I use only one and symlink them together. These are just some examples of how you can use alias. Anytime you find yourself repeating lengthy commands think about creating an alias. Optionally, if it's a long command you are only going to be using for a short period, be sure to use history tools like ! and !? to quickly repeat previous commands.