Increase Productivity with Bash: Aliases and Functions

I spend a lot of my time moving around in the Terminal with Bash which I find to be a lot faster than moving around in the Finder. However there are a few ways to tweak Bash to be even faster and increase productivity!

One such way is through an alias. Aliases are really nothing more than a shortcut, an abbreviation, and a means of shortening a long command that you use quite often. Here are two examples of implementing aliases:

alias scom="svn commit"

and

alias ss='./script/server'

If you use Subversion or Rails you’ll use either of those commands often and it’s nice to be able to shorten those long string into only 2 characters. Aliases can also do multiple commands like so:

alias cls='clear; ls'

Just add in a “;” between commands.

As good as aliases are, they are static and limited; you’re stuck with those commands and can’t give any arguments. Here’s where Bash functions come in. A function is a subroutine, or a block of code that implements a series of operations. You should consider using a function whenever there is repetitive code, or you often repeat a task with little variations. One example of a function that I use is:

cd() { builtin cd "$1"; ls; }

The “cd” at the beginning is the name of the function and what when typed in the Terminal will signal to perform that function. If you’ve done any programming you’ll know that the “()” signifies that it is a function. You then put the operations in the “{}”. The “builtin cd” just runs the normal “cd” command, the “builtin” is useful when you wish to rename a shell builtin to be a function, but want to use the functionality of the builtin within the function itself. The “”$1″” is the only dynamic part of this function, this is the argument that we are passing to the command, for example if I were going to change to my Documents directory; when I type “cd ~/Documents”; “~/Documents” would be put in place of the “$1″. Again the “;” just means that it is the end of that command and finally print a list of the working directory with “ls” and another “;” to end that command.

function_example

So as you can see aliases and functions can be quite useful and I recommend you implement your own to become more efficient.

Note: For those who don’t know you put aliases and functions in your ~/.bashrc file.


 
 

One Response to “Increase Productivity with Bash: Aliases and Functions”

  1. I use aliases for accessing long directory structures I use frequently.

    Something like: 'alias cdmt=cd /usr/local/share/content/sites/bjasper/mactips/public_html'

    I also use it for accessing the mysql server: 'alias devsql=mysql -u user -p db.server.com dbname'

    One of the common functions I use is a shortcut to find:
    ' ff () { find . -exec grep -q "${1}" '{}' \; -print; }`

    Update: Corrected the find function to the actual function I use.

Post a comment to "Increase Productivity with Bash: Aliases and Functions"



Support MacTips

MacTips Newsletter


Random Popular Tips

Recent Comments

Recent Discussions

MacTips Poll

What level of tips would you like to see?

View Results

Loading ... Loading ...

Friends of MacTips

Subscribe to MacTips

Site Links