Aliases


Bash Aliases

Early on, I learned that you can create a file called .bash_aliases. My most common use for these was to alias a single command to a “cd”. For instance, I typically put all of my projects in a folder under Desktop and call it “projects”. So, if my current project was in a folder called “managing_workspaces”, I would have to “cd ~/Desktop/projects/managing_workspaces”. That can be a lot to type over and over. It is also not always easy to spell correctly, though command-line completion helps, unless I have two projects with similar names. With aliases, I can alias the whole “cd ~/Desktop/projects/managing_workspaces” to “mw”.

The biggest problem with aliases is to edit it you have to edit the file and then source it. And usually there is more than one terminal where you have to source it. And you have to either update all the terminals at once or as you run across them. Or you can close all your terminals and re-open them.

The other issue is that aliases don’t really accept arguments.

Shell Functions

Shell functions allow you to assign a function name to some logic. For instance, it is common for projects to have a common folder structure where only a part of the path changes. As an example “/mnt/c/dev/apps/tomcat8/bases/company221/logs”, the “221” changes depending on the project. a shell function could be something like

logs() {
  cd '/mnt/c/dev/apps/tomcat8/bases/company'$1/logs || return
}

So, to change to the logs directory of company 222, you would execute

logs 222

Once again, you have to source the file where you store your shell functions whenever they change and you have to load them at some point into your terminal.