Skip to content

Instantly share code, notes, and snippets.

@ArmaniH
Created January 3, 2016 21:55
Show Gist options
  • Save ArmaniH/4a8c1108afdca7a57f3d to your computer and use it in GitHub Desktop.
Save ArmaniH/4a8c1108afdca7a57f3d to your computer and use it in GitHub Desktop.
Bash Profile Tutorial

#Bash Up[dating]! ##Creating and updating your Bash Profile December 30th, 2015

##Why Update Your Bash Profile? Besides just customizing the look of your Bash profile, updating its functionality can allow you to change your terminal prompt, change the colors of text, add aliases to functions you use all the time, and much more. So let’s get started... Accessing your profile on a Mac:

In the user directory there is a file called “.bash_profile” that contrails the configurations and user preferences for the command line. It is the automatically loaded whenever you open a new terminal window.

Accesing the .bash_profile is simple enough. So simple that there are multiple ways to go about it. You can edit your .bash_profile directly from the terminal window or open it with a text editor and edit it that way. It really comes down to personal preference. I’ll cover both ways (as they are mostly similar). ###Text Editor Method: Open up Terminal and enter in open -e .bash_profile. This will open your profile in a simple text editor. Functions like saving changes work as they would for any normal Mac program, with the docked menu bar. So exiting the editor would just be cmd+q.

###Terminal Method: Open up Terminal and enter in nano .bash_profile. This will open your profile in a the terminal window. Example: alt text

##Editing You .bash_profile... The exciting aspect of making your profile your own is that there are dozens of options to customize it and even more combinations you can use to truly make it your own., so we are going to focus on two areas I feel are the most useful for navigation; indicating when you are in a git and what branch you are on and a little bit of color highlighting to made the command line stand out from the output.

First things first: Go here and get the git-promt.sh script. Put it somewhere like ~/.git-prompt.sh. Set it and forget it. Find PS1= in the file. This little section determines what your prompt will look like. Now we can add \$(__git_ps1) to your prompt. It can go anywhere really. This is mine. Very minimalist, I know. It’s the Swedish in me. PS1='\w$(__git_ps1) \n\$ '

Always remember to open and close your prompt with single quotes. You can save and exit. You’ll need to reload your terminal to see the effects that place. After reload, head on into a git repository and see your branch! That’s all there is to it!

###Adding color to your prompt: The terminal preferences menu gives you some options to spice things up with your general terminal appearance and that is a lovely feature, but what we’ll be doing is digging deeper to give custom color properties to our bash prompt. Let’s go back to our profile: Remember there are two ways to get to our profile, nano .bash_profile and open -e .bash_profile. Once you are into your profile, find PS1=. There are A LOT of color options you can use to highlight your command line. Color codes are places before the prompt character they are effecting. So for example if I wanted the username to be purple it would look like \e[0;35m\]\u.

The crucial portion of this string are the 0;35m. The first number and semicolon indicate whether the affected command line portion is standard, bold or underlined. 1; would produce a bold effect; 4; an underline, with 0; have no text effect. If you were to remove the first digit and semicolon all together the color effect would become a background color, so \e[35m\]\u would produce a purple highlighted string on the command line, with default colored text, which will be black.

It is important to note that all things that are opened must be closed. \[ is the opening sequence for characters that aren’t to be displayed in the prompt. \] is the corresponding closing sequence. How about we put it all together.

So here is my final prompt: PS1='\[\e[0;32m\]\u\[\e[m\]\[\e[1;34m\]\w\[\e[m\]\[\e[m\]$(__git_ps1)\e[0;34m\]\e[0;32m\]\n\$ \[\e[1;30m\]'

Which looks like...

alt text

There are much more ways to set up your bash profile. Give it a shot, have fun with it! I hope this was helpful.

###Helpful Links: Wondering what all those /w and /u mean? Go here for a lovely breakdown of bash prompts.

Feeling more chromatic? Here are your Bash color codes.

© Armani Hall | @Charmani226 | armanihall@gmail.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment