One Quick Trick to Save You Hours in the Terminal Shell
A.K.A. “How to Become a Bash Wizard”
If you’ve ever opened a command line shell, you are likely aware how repetitive and tedious performing even simple tasks can become. Yes, the command line holds great power, representing a beautiful and direct interface with the machine… but let’s be honest: it can also be the source of many headaches.
Note: This post will focus on Unix-like systems like Linux and MacOSX.
Imagine you’re working on a new programming project, and you just want to open and run the code.
First you would need to change to the correct directory:
$ cd ~/long_annoying_path_to_my_code/
Then you might want to open your source code in a text editor:
$ vim my_source_code
Finally, you would run the program:
This might not seem too bad, especially if you know your way around the command line pretty well. If you wanted to stop the program, and start it again, you could simply type control + C to halt the program, press the up arrow key to recall the last command, and hit enter. Problem solved.
But what about when you exit out of the shell? What about if you want to open and run your project multiple times a day? What if running your program requires calling additional commands? Do you really need to type in everything over and over again?
Luckily, the answer is no. The rule of thumb in computer science is that if it’s boring and repetitive, it can be automated!
The secret lies in two hidden files: .bash_profile and .bashrc
In MacOSX, these files are found at the paths:
In Linux, they are usually found in the home directory:
If these files don’t already exist, you can always make them using touch after navigating to the correct directory:
$ touch .bash_profile$ touch .bashrc
Note: The dot before the file name makes these “hidden” files, so if you are attempting to find them with a GUI, make sure that the setting to show hidden files is set to true on your system. If you are using the command line exclusively, try opening the files by name in a text editor before deciding they aren’t there.
These files provide a basis for customizing your entire bash experience, and hold so much power that I could write an entire book listing all their potential uses. However, for our purposes here, I will specifically focus on one feature: aliases.
Aliases allow you to run long commands in the terminal shell by typing in short keywords that you define.
Think of aliases like magic spells. Once you set everything up correctly, you can call upon complex functionality with a simple incantation. “Abracadabra!”
The .bashrc file is where the real magic happens. However, we first need to configure the .bash_profile file to automatically load up our magic rituals. When a new terminal interface is opened, .bash_profile is called automatically, defining all the customizations for our bash experience. In order to load our magic spells, we first need to tell the .bash_profile to look in the .bashrc file for our alias definitions.
To do this, open up the .bash_profile in your preferred text editor:
$ vim .bash_profile
Next, add the following line of code to the top of the file:
Finally, save and exit (:x if you are using vim). Now it’s time to have some fun with .bash_rc
First, open the .bash_rc file in a text editor:
$ vim .bashrc
Now, let’s go back to our example from the beginning of this post. Instead of typing every command by hand, what if we could make everything run simply by typing a short keyword like runproject into the terminal shell? With a little magic, we can! To do this, just use the keyword alias, followed by the commands you want to execute. If there are multiple commands, you can separate them with semicolons. Finally, surround the group of commands with double quotes, and assign them to the keyword with an equals sign.
That is, you would write:
alias runproject=“cd ~/long_annoying_path_to_my_code/; vim my_source_code; ./my_project_name”
Next, save and exit the .bashrc file, and close out of your terminal shell. When you open a new window, .bash_profile will refresh, and load in your new alias from .bashrc
Now all you need to type is:
…and your project will run! You are starting to become a bash wizard.
I’m sure you can already see how much time this type of automation could save you, but what if you wanted to run dozens, or even hundreds of commands? You could technically write these all in .bashrc, separated by semicolons, but this would become unmanageable once you define more than one alias (which you will). Instead, another approach is to group larger collections of commands into shell scripts, and run these scripts using aliases!
First, make a new folder in the Documents directory to contain your shell scripts:
$ cd ~/Documents$ mkdir my_scripts
Create a shell script to contain the runproject code.
$ vim runproject.sh
In your text editor, add the following line to the top of the file:
Next, add the commands from before, this time without semicolons. The file will now look something like this:
After you save and exit, you need to make sure the shell script is executable:
$ chmod +x runproject.sh
Finally, navigate back to your .bashrc file.
Now, we can simply run the corresponding shell script whenever the keyword is typed. To configure this, open up .bashrc in a text editor, and change the code to the following:
This shell script solution works best for scripts with many commands, where defining all the specifics in the .bashrc file would be too messy.
Hopefully you are beginning to see that the possibilities for automation are literally endless. You can even automate the process of automation. Are you sick of repeatedly typing the path to your shell scripts? Solve it with an alias!
alias scripts=“cd ~/Documents/my_scripts”
Any task in the command line that is dull or repetitive is an excellent candidate for alias magic.
The scripts you run don’t even need to be shell scripts. Anything you can run from the command line, you can run with aliases! There is also the possibility of calling scripts that accept positional arguments, or making use of tools like GREP or AWK. I personally have an alias that lets me watch a command line text animation of Star Wars Episode IV whenever I want.
The only limit is your imagination, so get out there and start automating every part of your terminal shell workflow. You will be surprised at how much time you can save yourself. Congratulations: you are now an official Bash Wizard!
If you are interested in more computer science content, check out my YouTube channel! https://youtube.com/channel/UC3QAtE9qAys9385wFzs8hNA
I also offer private lessons over Zoom and Skype! Feel free to contact me at firstname.lastname@example.org or visit my website at http://devonhubertcoding.weebly.com