When you use a command line interface a lot, there would be some commands you use every day. Some commands are short but the others could be a bit longer. One reason is that one command runs with several parameters. Another reason is that one command can consist of multiple commands with some operators (e.g. |, :, &&). In any case, that is cumbersome to type the same command, especially for longer ones.

Bash shell has two ways for this: alias and function. Alias is a simple way to create a shortcut. The format for creating it and some examples follow:


# alias name='command'
alias gbr='git branch --remote'
alias gco='git checkout'
alias l='ls -lah'

To run the alias, type the alias name in your bash shell.

$ gbr
  origin/HEAD -> origin/master

Most aliases contain every information to run the command. Thus, the name would be the only thing you need to type. In some cases, however, you want to run it together with parameters. gco is the one example. You want to shorten the typing instead of git checkout and you also want to specify the branch when you run it. For that, you just need to type parameters after the alias. Commands below works exactly the same.

$ gco release
$ git checkout release

Alias acts as replacing a name to a defined value. Although it is quite handy for most cases, you might want to do something more advanced where alias doesn’t fit well.

First, you cannot specify the parameter position with alias. Alias always put parameters at the end of the defined command. Below commands has the same command and option except for MainActivityTest and PlayerActivityTest. If you want to replace that part, alias doesn’t support it

$ ./gradlew :app:testDebugUnitTest --tests="*.MainActivityTest"
$ ./gradlew :app:testDebugUnitTest --tests="*.PlayerActivityTest"

That is the time you need to consider functions. Function allows specifying the parameter position. Parameters are referred with numbers followed by $ character.


# function_name() {
#   body
# }
ut() {
  ./gradlew :app:testDebugUnitTest --tests="*.$1"

To run the function, you type the function name. By seeing the command, it is hard to know whether it runs an alias or a function.

$ ut MainActivityTest
$ ut PlayerActivityTest

Second, alias requires to escape special characters. Let’s see the below example. With awk command, that command shows the male employee names only.

$ cat data
  m broderick
  f amelia
  f julie
  m samual

$ cat data | awk '$1 =="m"{print $2;}'

If you make an alias, you need to wrap with a single(or double) quote the command. It would be like this:


alias male='cat data | awk '$1 =="m"{print $2;}''

When the alias is loaded, there is an error. The reason is that there are single quotes in the value.

=m{print not found

To escape it properly, you need to put '\'' instead of \' because of the bash interpretation.


alias male='cat data | awk '\''$1 =="m"{print $2;}'\'''

Although it is doable, the original command gets hard to read and maintain. Function fits better in this case. Just make a function and put the command in the function body. You don’t need to worry about escaping anymore!


male() {
  cat data | awk '$1 =="m"{print $2;}'