Adding Bash Aliases in OS X

Posted on Posted in Mac OS X

Opening a Terminal in any Unix-based OS will generally present a user with a bash prompt. Even though the bash prompt excels at interactivity, supporting command line editing, completion, and recall, there are still ways to customize it to each user’s specific needs and preferences. One such way is by creating bash aliases.

Bash aliases reside in a file located at ~/.bash_profile and are “shortcut” commands that you create. These “shortcuts” are aliases for other — often much longer — command(s).

To begin, change to your home directory and open the .bash_profile file in your preferred text editor. Alternately, you can include the homepath in your command to open the file.

1
nano ~/.bash_profile                                                                         

Within this file, the format for adding aliases is as follows:

1
alias AliasName='Command1;Command2'                                                          

For example, if I want to create an alias called ssh-dallas which opens an SSH session over port 465 (since many firewalls block port 22 egress traffic for normal users) with my server in Dallas and have it sign in as me so that all I need to do is enter my password, I would write the following in my .bash_profile.

1
alias ssh-dallas='ssh [email protected] -p 465'                                                

Save the file and exit your text editor. To refresh the changes, reload the .bash_profile.

1
source ~/.bash_profile                                                                     

Now, at a bash prompt, entering “ssh-d” and pressing the Tab key to activate auto-complete should complete my command to “ssh-dallas”. Executing the command will give the following output, just as it would if I were to type the entire command like normal.

Jacobs-MacBook:~ jacob$ ssh-dallas
[email protected]'s password:

So, there you have it, a neat and tidy way to minimize the number of keystrokes you make and, with luck, a quick way to help streamline your day-to-day Terminal endeavors.


Leave a Reply

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