Bash Profiles (What’s great about them and how to make them better)

Peter Traversa
4 min readAug 3, 2018

--

Your bash profile is an incredibly powerful tool that can make your terminal infinitely easier and quicker to use. It can improve your efficiency, help you avoid time-wasting errors, and just look real pretty on your screen.

Have you ever had your desktop set up perfectly with your IDE and terminal sized perfectly, exactly where you wanted them, got in a serious coding groove and then typed “exit” in your terminal by accident? Yeah, me neither.

Looking at possible solutions for a mistake I never, ever, ever make brought me to the bash profile. Now when I type “exit” by accident, my terminal prompts me with:

The frustration-saving snippet of code lives in my bash_profile and looks like this:

What is your bash_profile?

bash_profile is a configuration file for bash shell, which you access with your terminal on a Mac. When you invoke bash with a login, it will search for and load ~/bash_profile and all of the code contained within.

Before you go about making any changes and customizations to your bash_profile, I recommend backing up your profile by copying and saving the file as bash_profile.bak.

When you do get around to making customizations, and you want to see them implemented right away, instead of having to logout of your terminal and open a new window you can simply type “source ~/.bash_profile”.

Prompt Customizations

You can change your prompt in bash_profile. Did you wish that your computer prompted you with a cool nickname? I didn’t, but I did find some cool ASCII characters that I thought might look neat.

Your prompt can be customized not only with a character or string, but also with your current directory, the current time in 12 or 24 hr time, the date and time in multiple formats, your username, the computer name, your shell version, etc. If you want to design your prompt manually, https://www.gnu.org/software/bash/manual/bashref.html#Controlling-the-Prompt has a comprehensive list of your prompt options. There are aso several great bash_profile customization websites that will write out a PS1 script for you. My favorite is http://bashrcgenerator.com/. The site will provide the code to build your prompt with multiple elements, add custom characters, and generally make building your prompt foolproof.

If you consider yourself a color connoisseur or have serious feelings about what the text of your terminal should look like, bash_profile is the place to exercise your inner decorator. You can define local colors

and use them to change the color of your prompt text in PS1 to match your terminal background, your phone case, or whatever color shirt you happen to be wearing that day.

And most importantly, shortcuts

Let’s say you’ve listened to your instructors and are committing code to GitHub at a rate of 5–10 times an hour, or every time you write a new method. That’s a lot of adding, committing, and pushing, branch-switching, and all of the typing that goes along with it. As a future engineer, I’m looking to always be as efficient as possible.

Anywhere you can cut out keystrokes with shortcuts, you should do it. Thus, git aliases.

Why type “git add .”, when I can just type “ga”? I type two simple characters and save a half second at the same time. It might not seem like much, but once you take notice of the commands that you type in your terminal the most, creating aliases can save you a good amount of time in the long term. Aliases can be created for any command you find yourself typing often in your terminal. In addition to saving time, typing “gcm” instead of “git commit -m” greatly reduces the possibility of my mistyping the command, throwing my hands up in frustration, and retyping.

Part Two Coming…

Prompt customization and aliases are just scratching the surface of what your bash_profile can do for you. With simple scripts, you can extract files from any source, shortcut network options, and generally make your life easier. Even if you only run the learn.co script for their specific profile customization at https://github.com/learn-co-curriculum/bash_profile, it’s a solid foundation that you can build upon.

My next post in this series will get further into script customizations for your bash_profile.

--

--

Peter Traversa

Fullstack web developer with a passion for designing engaging user experiences through code.