Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
    
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 Jul 15 '12 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 :) –  MHaaZ Jul 15 '12 at 22:55
    
Possible cross site duplicate of: unix.stackexchange.com/questions/30925/… –  Ciro Santilli Mar 16 at 18:08
add comment

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
    
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. –  Scott Severance Jul 16 '12 at 23:12
add comment

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

Alias do work only in an interactive console, they are not meant to be used in scripts.

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

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

Function are much more powerful than aliases, they can be used to build very complex programs.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.