Set up Bash and crate handy shortcuts

In the section Life in the Terminal, I introduced the concepts of the .bashrc and .bash_profile documents. When working with the Terminal.app, you will use these files extensively to maintain configurations for your Bash apps.

As described, the easiest way to manage these documents is to make the .bash_profile aware of the .bashrc file. Now that we have Terminal.app setup so that you have an alias to open files from the command line, from your home directory, cd ~/ you should be able to run the following to open the .bash_profile document in Sublime:

$subl .bash_profile  Now that you have this document open, make sure that the following statement is entered near the bottom of the document: if [ -f ~/.bashrc ]; then source ~/.bashrc fi  What's really happening here is the statement source ~/.bashrc. source is a function that will recycle the .bash_profile and .bashrc documents inside Bash. Doing so will ensure that all the scripts in these documents are loaded into Bash and available for your use. The PATH variable Another important aspect of these files is setting the PATH variable. PATH is an environment variable telling Bash where executable scripts are located. If there is no PATH set, at the very least you need to have the following: PATH=/usr/local/bin:$PATH


With Unix operating systems, additional location directory paths can be added keeping in mind the colon : separator. DOS, OS/2, and Windows operating systems on the other hand uses the semicolon ; character.

Using Alias commands

A power tool that developers make use of with .bash_profile and .bashrc to make life easier is to use the alias feature to create shortcuts to commonly used commands.

Adding an alias is easy, just make sure to add the alias AFTER the PATH variable in either file you choose to use. The structure is simple to follow:

alias <name>= "<action>"


A great example is to create a shortcut to your Dropbox folder. Dropbox is typically located at this path:

$cd ~/Dropbox  You could create an alias of drop which would quickly take you to your Dropbox directory from anywhere on your computer: alias drop="cd ~/Dropbox"  Now in Bash you simply need to type on the alias and you will be immediately moved to that directory. $ drop


Use the pwd command to get your current location:

\$ pwd


Concatenating commands

A great feature you can take advantage of is using the && function to concatenate commands in an alias. Say for example you want to update the ls command to be more feature rich like, clear the view in Bash, list the contents and illustrate the difference between files and directories?

alias list="clear && ls -p"


Once an alias is created, this alias can be used within another alias you create. For example, let's say that you want to update your drop alias to make use of this new list alias?

alias drop="cd ~/Dropbox && list"


Conclusion

Using the alias feature I feel is a huge win in development. There will be things that you need to remember and those that simply require the use of an alias. Instead of jotting commands down on a sticky note somewhere, make it an alias. Not only are you making it easier to remember, but this is a consistent resource of commands that you never have to say, "Where did I write that down?"