Creating Mac OSX Aliases

Adding an alias to your system is a good way of saving time when remembering complex commands. You can reduce a command down to a quick two letter command that is easy to remember.

The alias command can be used to assign an alias on the fly. You can create an alias to 'ls -lah' by typing in the following into the command line.

alias ll="ls -lah"

Now, when you type 'll' (i.e. two lower case L's) you will actually run the command 'ls -la'.

Or you might want to do more complex things like running your selenium server.

alias selenium-server="java -jar ~/Development/selenium-server-standalone-2.25.0.jar"

To remove an alias you can use the unalias command to remove an alias from your system.

The unfortunate thing is that when you reset the terminal you will lose any aliases that you created. To create aliases more permanently you can add them to your .bash_profile file. This file should exist in your home folder but if it doesn't then you can easily make it using the touch command. This file is used to store personal settings like any aliases you want to keep.

You can add any aliases you want to this file, but once you do you need to run the source command to reload them into the current terminal scope.

source .bash_profile

You can see what aliases you currently have on your system by typing in the alias command on it's own.

It would be great if aliases were allowed to accept parameters, but for the most part they don't. You can pass an argument to an alias, but it will always be appended to the end, which isn't all that useful. For this reason another method must be used. Take this script that checks to see if a server is online, which I stole from commandlinefu.com. It will ping a server once and return it's online or offline status as a single word.

ping -c 1 -q MACHINE_IP_OR_NAME >/dev/null 2>&1 && echo ONLINE || echo OFFLINE

You can put this into a function called online and drop it into your .bash_profile file. On OSX/Darwin you need to prefix the function definition with 'function' in order to prevent any syntax errors. To pass in the argument for the server name or IP address you need to add $1. You can pass in further arguments with $2, $3 and so on, but we only need a single argument for this function.

function online() {
ping -c 1 -q $1 >/dev/null 2>&1 && echo ONLINE || echo OFFLINE
}

With this in place you can now quickly check the status of any machine or website by using the online command followed by the machine you want to check. The following will check for the status of www.hashbangcode.com.

online www.hashbangcode.com

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
1 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.