Diving into the Bash Profile

Today at Flatiron we configured our dev environments on our computers, and learned a lot about what’s going on in the Terminal and Bash. While we’re not (yet!) command line wizards, I wanted to delve a little more into the secrets of the Bash profile, what it is, and how it can help make life easier.

The home directory (you can type ‘cd ~’ if you get lost jumping between directories and need to get back there) has a bunch of configuration files. If you type ‘ls’ in the command line, you’ll see the normal documents and directories that are listed there, but no special config files. That’s because they’re hidden! In order to de-mystify them and make them visible to mere mortals, you need to invoke the flag ‘-a’. So ‘ls -a’, or ‘ls -la’ depending on your viewing preference, will list out all of the hidden files as well as the normal ones. You can think of any file that begins with a ‘.’ as having super powers. These are the normally-hidden ones.

Depending on your operating system, you may or may not see one particular file named ‘.bash_profile’. This is the file that bash will look for first when it opens, and which tells bash what the settings should be. If you don’t have it, don’t worry, you can just make one by typing: touch .bash_profile

Once you find or create the .bash_profile document, you can go ahead and open it with your text editor. (I prefer Sublime 2.) This is your profile.

There are a variety of things you can put in your profile to change the behavior of Bash. Just remember that any time you make a change to the .bash_profile file, you need to tell Bash that you made the changes before they will take effect! You can do this by closing and opening the terminal, or you can type: source .bash_profile to reload the profile.

Now I can get to some of the handy things you can do in your .bash_profile file! To do any of these, you just have to type the code into .bash_profile, save, and reload the profile.

1. You can add to the PATH!
The path is what Bash uses as a guide to see where it should look to find a program when you run it from the command line. You can see the current Path by typing echo $PATH in the command line. You should see something like:
The important parts to note are that this is a list of directory locations separated by colons, and that Bash will look through them in order to find the program it’s looking for. So let’s say you want to add other locations to the Path. What do you do? Well, you can simply add them to your profile by writing something like this:
export USR_PATHS="usr/local:/usr/local/bin:/usr/local/sbin:/usr/bin"
This will add the new locations to the beginning of your Path.

2. You can make sublime your default text editor!
Ever make a commit to git and forget to add the -m flag? Vim pops up as the default text editor. But unless you’re familiar with the editor, it can be difficult to make changes, let alone save and escape! (For reference, typing “:wq” will save and escape.) But, with the .bash_profile you don’t have to put up with this! Yes, you can make your default text editor Sublime! Or whichever you prefer. To use Sublime, add the following:
export VISUAL="subl -w"
export SVN_EDITOR="subl -w"
export GIT_EDITOR="subl -w"
export EDITOR="subl -w"

If you don’t have the subl command line shortcut set up, the documentation is here. You can set up the shortcut by typing the following into Bash:
sudo ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" /bin/subl

3. You can create functions to jump straight to a specific directory!
I don’t know about you, but I have a code file that I use all the time! But it’s nested a couple of levels deep and it gets annoying to type cd Documents/projects/code all the time! (Alright, you caught me, I actually have a much more convenient location for where I keep my projects, but this makes for a better example!) What if I want to be able to jump straight to my code directory from anywhere? Well, I can create a function in .bash_profile to do this!
function code {
cd /Users/$USER/Documents/projects/code/$@

Now in order to go to my code folder, I just have to type “code” in Bash to call the function. Because I defined the directory as an absolute path, and because cd is part of the function, it takes me right there, no matter where I am in my directory system! This can save you from digging around through layers of folders.

4. You can create aliases!
If you find yourself typing the same Bash command over and over, there’s a quick and easy way to give yourself a shortcut to help yourself be a bit lazy. This is called aliasing. Let’s say you’re tired of writing “ls -la” whenever you want to list the items in a directory. Well, let’s shorten it to just having to type “l”. To do this, just add the following to your .bash_profile:
alias l="ls -la"
And then you’re all set!

There are so many other ways to customize your Bash Profile, and I’m sure there are plenty I haven’t even scratched the surface of yet. I look forward to learning more, but I think these are a good start!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>