When to use an alias and when a function in Bash

October 19th, 2011 - 09:13 am ET by Cecil Westerhof | Report spam
I use a lot of aliases and functions in Bash. Today I defined the
following alias:
alias zypper_ps="zypper ps | awk 'BEGIN { FS = \"|\" } ; { if (NR > 4) {if (\$5 !~ /^[ ]*$/) print \$5} }' | sort | uniq"

I think that in this case an alias is the right way to do it. But I
was wondering are there rules of thumbs when to use an alias and when
a function. (Of-course if you need a parameter, you need a function.)

And is there a (significant) difference in performance?

Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
email Follow the discussionReplies 6 repliesReplies Make a reply

Similar topics

Replies

#1 ibuprofin
October 19th, 2011 - 03:25 pm ET | Report spam
On Wed, 19 Oct 2011, in the Usenet newsgroup alt.comp.linux, in article
, Cecil Westerhof wrote:

I use a lot of aliases and functions in Bash. Today I defined the
following alias:



Yes, I saw your post in alt.os.linux.suse

I think that in this case an alias is the right way to do it. But I
was wondering are there rules of thumbs when to use an alias and when
a function. (Of-course if you need a parameter, you need a function.)



'man bash' specifically, the section relating to "Aliases". At the
bottom of that section (just above the next section that relates to
"Functions") you will find

For almost every purpose, aliases are superseded by shell functions.

Functions are more versatile.

And is there a (significant) difference in performance?



Depends on what they are doing.

Old guy
Replies Reply to this message
#2 unruh
October 19th, 2011 - 03:51 pm ET | Report spam
On 2011-10-19, Moe Trin wrote:
On Wed, 19 Oct 2011, in the Usenet newsgroup alt.comp.linux, in article
, Cecil Westerhof wrote:

I use a lot of aliases and functions in Bash. Today I defined the
following alias:



Yes, I saw your post in alt.os.linux.suse

I think that in this case an alias is the right way to do it. But I
was wondering are there rules of thumbs when to use an alias and when
a function. (Of-course if you need a parameter, you need a function.)



'man bash' specifically, the section relating to "Aliases". At the
bottom of that section (just above the next section that relates to
"Functions") you will find

For almost every purpose, aliases are superseded by shell functions.

Functions are more versatile.

And is there a (significant) difference in performance?



Depends on what they are doing.



Uh, I think he means doing the same thing. Such as the example he gave.

Aliases simply replace a short phrase in the command line with a long
phrase. They are purely a syntactic substitution. Functions have greater
functionality. But so what. If you want to define
alias l "ls -la"
so that you can type l whenever you want to do ls -la, aliases are far
better. Writing that as a function would be silly. If you want to
accomplish some more complex task, functions may well be better. And I
doubt that either is better or worse than the other. Writing a
complicated alias can often lead to contortions needed to fit in with
the simple syntactic substitution of aliases.



Old guy
Replies Reply to this message
#3 Alexandre Aguiar
October 22nd, 2011 - 02:37 pm ET | Report spam
on Quarta 19 Outubro 2011 11:13, Cecil Westerhof said to people at
<alt.comp.linux>:

was wondering are there rules of thumbs when to use an alias and when
a function. (Of-course if you need a parameter, you need a function.)



My rule of thumb comes from C/C++. Aliases have similarities with inline
functions and macros. Aliases fit in shell scripts in the quite same way
inline functions and many times macros fit in C/C++ code.

If you are concerned about performance, you can use a script to test
excution times of other scripts (or any other programs you wish). This kind
of test can be sophisticated but a non pretentious benchmarking can be done
with something as simple as:

#!/bin/bash
test_times0
# first parameter is one of the scripts to test
test1="$1"
# second parameter is the other script to test
test2="$2"
shift 2
# other parameters are passed to the scripts in test
# unique name for this test
output="$(date +%Y%m%d%H%M%S)-$test1 x $test2.csv"
{
for test_count in `seq 1 $(($test_times+1))`; do
( time -p $test1 "$@" ) 2>&1| tail -n 3 | head -n 1 | awk '{printf "%f",
$2}'
echo -n ";"
( time -p $test2 "$@" ) 2>&1| tail -n 3 | head -n 1 | awk '{printf "%f",
$2}'
done
} | sed '1d' > $output


This will run one script at a time with the same parameter set. The double
run will be performed $test_times times. Execution times are captured and
saved to a csv file that you can open as a spreadsheet and compare means
with a "t" test, for instance. The script excludes the first observation
that can be severely biased by initial disk access.

Two warnings:
1. be sure to make enough tests to produce a reliable statistic; you
probably do not need more then 100 observations;
2. the smaller the difference, the greater $test_times should be;
3. you can use lower alpha error levels if you want to do a more stringent
test;

Use Statistics carefully to avoid deceits.

Enjoy!


Alexandre
Replies Reply to this message
#4 AMX
October 30th, 2011 - 07:14 pm ET | Report spam
On Wed, 19 Oct 2011 19:51:29 GMT, unruh wrote:

They are purely a syntactic substitution. Functions have greater
functionality. But so what. If you want to define
alias l "ls -la"
so that you can type l whenever you want to do ls -la, aliases are far
better. Writing that as a function would be silly. If you want to
accomplish some more complex task, functions may well be better.



As the example =:-)), you may want to pipe results of ls -la to
pager. Then function would be simple:

l(){ ls -la $@ | more; }

You may try to replace this by alias, but it is not that easy.


Writing a complicated alias can often lead to contortions
needed to fit in with the simple syntactic substitution of
aliases.



Right tool to right problem. Try to keep your code as simple as
possible, but not simpler.

AMX

adres w rot13
Nyrxfnaqre Znghfmnx
Replies Reply to this message
#5 unruh
October 31st, 2011 - 10:03 am ET | Report spam
On 2011-10-31, AMX wrote:
On Wed, 19 Oct 2011 19:51:29 GMT, unruh wrote:

They are purely a syntactic substitution. Functions have greater
functionality. But so what. If you want to define
alias l "ls -la"
so that you can type l whenever you want to do ls -la, aliases are far
better. Writing that as a function would be silly. If you want to
accomplish some more complex task, functions may well be better.



As the example =:-)), you may want to pipe results of ls -la to
pager. Then function would be simple:

l(){ ls -la $@ | more; }

You may try to replace this by alias, but it is not that easy.



So? Yes, there are cases where a function is better. Obviously that
command cannot be done by a simple syntactic substitution. The $@ makes
no sense syntactically, It needs to read the whole input to figure out
what that means. aliases mean "for the occurance os X as a command
substitute the string Y, completely literally."



Writing a complicated alias can often lead to contortions
needed to fit in with the simple syntactic substitution of
aliases.



Right tool to right problem. Try to keep your code as simple as
possible, but not simpler.



Agreed, and aliases have their place.


AMX

Replies Reply to this message
Help Create a new topicNext page Replies Make a reply
Search Make your own search