23

I want to know the difference between aliases and functions in the bash shell - and when to use which one.

It seems obvious that since functions can take arguments, they are a lot more powerful - since I work mainly on the road with a netbook and want to save energy and processing power, I would especially like to know a little more about what the performance differences of this two ways to extend bash functionality are.

3
  • 1
    No need to be self-demeaning. If your question not wanted, it will be downvoted. And if someone wants for you to clarify your question, he will say so in a comment. And forbidding people to edit your post is kind of missing the point. This site is designed for everything to be editable for everyone.
    – k0pernikus
    Commented Jul 15, 2012 at 16:05
  • I wasn't trying to forbid, but just kindly ask to give me a chance to clarify if they consider it necessary. I've had the experience of asking very simple questions about topics I know little about, and then had people think it's totally obvious I am asking about something more complex and correct me about how I don't know how to ask for what I want. Of course one of the great advantages of this platform is the fact it is editable by peers, I wasn't trying to undermine that. Thanks for your feedback, made me think :)
    – MaxAxeHax
    Commented Jul 15, 2012 at 22:55
  • Possible cross site duplicate of: unix.stackexchange.com/questions/30925/… Commented Mar 16, 2014 at 18:08

3 Answers 3

20

Think of aliases as nicknames. You might have a command that you perform a lot but want to shorten.

As an example, you often want to go straight to desktop in commandline, so you can do this

alias desktop="cd ~/Desktop"

From then on you just type

desktop

in terminal and it will perform the cd ~/Desktop for you.

Functions contains logic. In a function, you might make calls to several different programs. Here's a simple echo function

function e {
                echo $1 
                echo $1 $1
                echo $1 $1 $1                 
           }  

While it may appear similar to an alias when you call it

e Hello

Your e() can have a lot of different things happen. The above is a simplistic example.

Aliases should be reserved for simple use cases. Personal example - I have replaced my rm command like this

alias rm='trash-put'

Whenever I do an rm, it will send it to trash instead of deleting it from disk. This caters to my clumsiness in the terminal where I may (sometimes) accidentally delete an important file.

Functions, you need to remember, are pieces of logic. You wouldn't use a function standalone, usually. It would be part of a larger script. Imagine a script which takes all of your files and renames them to their pig latin versions. Ignore that there are different ways of doing it.

But what you could do is loop through every file in the directory and pass the filepath to your RenameAsPigLatin function. The RenameAsPigLatin function might have extra logic in there involving numbers, where you decide that files ending with numbers shouldn't be renamed.

Immediately you can see the benefit of having it as a function. The function can focus on the renaming by your strange rules while the rest of the script can traverse various directories as necessary.

2
  • 2
    In my opinion, there are many reasons to use functions standalone. In fact, I have a number of functions defined along with my aliases. For example, I've replaced cd with a function of the same name so I can have it automatically do an ls after each cd without sacrificing any functionality. This can't be done with an alias because it requires a bit of logic. Commented Jul 16, 2012 at 23:12
  • I use a lot standalone functions. Much better than keeping a lot of script files in a directory that needs to be added in the path
    – JB Jansen
    Commented May 7, 2020 at 10:14
7

An alias is a simple shortcut used in a console to avoid typing long commands or always repeating the same options.

A classical example would be:

alias ll='ls -l'

By default, aliases are only intended work in an interactive console. They are not meant to be used in scripts, though if you needed to do that, you could add shopt -s expand_aliases to scripts to enable alias expansion.

Functions can be used in scripts or in the console, but are more often used in scripts.

Contrary to aliases, which are just replaced by their value, a function will be interpreted by the bash shell.

Functions are much more powerful than aliases. They can be used to build very complex programs.

-1

As an addition to what’s already been said functions also pipe better than aliases where using an alias your locked into what’s defined by the alias (unless quoted). I have several stand-alone aliases that I call within task specific piped commands that would be much harder to call if they were aliases. Also functions allow you to set variables either globally or locally for the task at hand.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .