5

I did a short course on bash and terminal and one of the best practices laid out in the course was creating an alias for each function to be created. For example if I had a function:

function e() { echo "$*"; }

I should also create create an alias:

alias e='e'

Do you think it makes sense and if yes what is the reasoning? I figure I can source the functions and use them without aliases anyway so it seems kind of like unnecessary work.

3
  • I believe most programmers strive for performance, efficiency and readability. Maybe not in that order. Of course the re-usability of your functions is something to consider when it you evaluate what's best. After everything is considered, it comes down to a user's preference. I wouldn't create an alias or a function for something I would rarely use. Oct 25, 2016 at 21:19
  • The functions I occasionally create are very useful for me. The question is whether there's an advantage of creating an alias pointing to that function or simply using the function without an alias.
    – Yos
    Oct 25, 2016 at 21:41
  • i think this is a good topic though. I'll write an answer once i get to my laptop. This might be interesting Oct 25, 2016 at 23:25

2 Answers 2

7

Case for aliases

  • Aliases can be quite useful when you want to refer to a lengthy command by a short name.
  • In the order of precedence, aliases stand higher than functions, so in case where you want to override some existing name for your own session you'd use an alias.

Case for functions

  • When you have a command that requires dealing with single and double quotes at the same time, use functions. Those who deal with sed or awk a lot understand.
  • When you want to refer to a lengthy task consisting of multiple commands interacting with variables, use function. In general , I personally follow the rule that if there's more than 3 commands, it's time to use a function.
  • Aliases can be escaped by appending \. This is useful for system administrators. If you don't want your user to run certain command, a function will take precedence over . Example:

    $ ls
    serg says this command is a no-no
    $ \ls
    serg says this command is a no-no
    

    note that user can still run /bin/ls with no problems there, so it's not really a good security measure. I'd say the better use would be as a wrapper for some command, where you want to add a header or remove some information.

  • From bash man page: There is no mechanism for using arguments in the replacement text.If arguments are needed, a shell function should be used. Thus while you can do simple tricks - something like alias e="echo"; e Hello - you would want a function when you want to deal with command-line arguments extensively.

In my personal experience, I found myself using functions far more than aliases. They are like scripts, except it's not necessary to create external files - they can all live in my ~/.bashrc. Quoting and referencing variables become less of a problem.

7
function e() { echo "$*"; }

I should also create create an alias:

alias e='e'

That, as-is, is pretty dumb. There's no point to defining an alias of a function and giving it the same name.

I'd think that what they meant was:

  • Give every function you write a descriptive name, which can be long.
  • And make a short alias for convenience.

You must log in to answer this question.

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