Why would I create an alias which creates a function?

I found this answer too [U&L] In Bash, when to alias, when to script, and when to write a function? which explains the benefit of defining a function in an alias.

The benefit of doing so over declaring a function is that your alias cannot be simply overwritten by source-ing (or using .) a script which happens to declare a same-named function.


Here are my 2 cents on this and it represents my personal opinion as well as understanding on the topic.

  • Using aliases with functions is to some extent a personal preference of developers. I will add some differences between the two approaches, which may also account for personal preferences of using aliases vs functions
  • There are times when most of the things I want to do are possible with aliases itself but only a few require to take a parameter. So instead of mixing aliases with functions, I use an alias with the function itself

Example:

alias kgps='kubectl get pods --all-namespaces | grep '

This works great and I can search my kubernetes pods. Now for deleting these pods, I need to pass the same parameter but in between the command, so I use an alias with a function inside

alias kdp="_(){ kubectl get pods --all-namespaces  | grep \$1 | awk '{print \$2}' | xargs kubectl delete pod; }; _"

So most of my shortcut commands are possible to execute through aliases and only few which needs such things I use aliases with functions.

Aliases vs Functions

Now there are few differences between aliases and functions which I would like to highlight

Aliases can override system commands much more easily compared to functions

If I need to override ls, I can do that much easier with alias

alias ls='ls -altrh'

While a function equivalent of the same would be like below

ls() { command ls -altrh "$@";}
ls() { /bin/ls -altrh "$@";}

Aliases intention is mostly for shortcuts

Aliases are majorly used to create shortcut commands while functions are used for a lot of things, complex combinations of commands, auto-completion, bash prompts

Aliases are easier to manage

Run alias command you get a list of currently active aliases

$ alias
....
vs='vagrant ssh'
vu='vagrant up'
vus='vu && vs'
....

To get the list of functions we need to use declare -f or another similar command

$ declare -f | wc -l
  8226
$ alias | wc -l
  217

Now if I post a partial output of declare -f I get

$ declare -f
...
vi_mode_prompt_info () {
    return 1
}
virtualenv_prompt_info () {
    return 1
}
work_in_progress () {
    if $(git log -n 1 2>/dev/null | grep -q -c "\-\-wip\-\-")
    then
        echo "WIP!!"
    fi
}
zle-line-finish () {
    echoti rmkx
}
zle-line-init () {
    echoti smkx
}
zsh_stats () {
    fc -l 1 | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n20
}

As you can see there are lots of functions which are used but are not relevant to me. While the alias command gives me a very concise output and I can easily see what all is there. In my case, 100% of them are shortcut commands

Escaping aliases and functions syntax is different for system commands

To escape a defined alias you need to prefix it with \ while for functions you need to either use command <originalcommand> or absolute path of the command /bin/originalcommand

Aliases have higher priority over function

Look at the below example

alias ls='echo alias && ls'
$ ls() { /bin/ls -al }
alias
$ ls
alias
total 23173440
drwxrwxr-x+ 255 tarunlalwani  staff        8160 Jul 30 22:39 .
drwxr-xr-x+ 113 tarunlalwani  staff        3616 Jul 30 23:12 ..
...

As you can see when we run the ls command, first the alias is used and then the next ls is calling the function.

This becomes also a way of wrapping an exiting function with the same name and re-using the original function inside as well, which can only be done using alias and promotes the format in the question


I found an Ask Ubuntu question about a related topic where one of the answers alleges that this is a misunderstanding of a different design principle: give the function a long and descriptive name, and create a shorter alias for convenience.

This still offers no insight into why you would have the alias redeclare the function every time.

Tags:

Shell

Bash

Alias

Sh