[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Functions or aliases?



* (radsky@ncia.net)[20040509 18:12]:
> Vincent Lefevre wrote:
>
> > On 2004-05-08 22:00:38 -0400, alex wrote:
> >
> >> alias win+='mount -t vfat /dev/hda1 /mnt/hda1; cd /mnt/hda1; ls -aF
> >> --color=auto'    (This is located in   /root/.bashrc)
> > [...]
> >
> >>I've read that aliases should be limited to simpler
> >>commands such as:
> >>     alias cd..='cd ..'  or  alias lsl='ls -l'
> >> and that aliases like my win+ should be structered as a function
> >> instead of an alias.
> > I agree.
> When I first read about functions, I used them but then I tried
> aliases to see how they worked and never went back to functions.
> Aliases are more compact, easier to set up, and adapt to changes.
>
> All I can say is that the aliases that I use have worked very well for
> me and I could't detect any difference in the results between the
> aliases and functions.  Could you please elaborate on why functions
> are preferable to aliases for compound commands?
> >
> >>I've tried the same command in function form and I don't see any
> >>difference.
> > win+ 2> error
> ???
> > makes a difference (when something is sent to stderr).
> ???
> > So do things like:
> > [[ condition ]] && win+
> ???
> >
> All I know about Linux has been gleaned from the lists and web
> pages so this is a bit too cryptic for my meager education. Could you
> please give a few details or point me to where
> there's an explanation?

Maybe this (admittedly cryptic) example will help:

vineet@quesadilla:~$ alias foobar='echo foo; echo bar'
vineet@quesadilla:~$ foobar
foo
bar
vineet@quesadilla:~$ : && foobar
foo
bar
vineet@quesadilla:~$ : || foobar
bar
vineet@quesadilla:~$ foobarfn () {
> echo foo
> echo bar
> }
vineet@quesadilla:~$ : && foobarfn
foo
bar
vineet@quesadilla:~$ : || foobarfn
vineet@quesadilla:~$

At least you should be able to see that there's a
difference.  Now let me try to explain the difference you're
seeing, in case it's not clear.

"A && B" is a shell idiom meaning "try A, and if
it succeeds, then also do B".  Similarly, "A || B" means
"try A, or if that fails, then do B".
':' is a bash builtin which always evaluates to a success.

So ': && echo foo' should always echo foo, and ': || echo
foo' should never echo foo.  So as a user, I'd expect
'some-test && win+' only de 'win+' if some-test succeeds.
As we see in my foobar example above, that's not how it
works with an alias, whereas a function works as expected.

Vincent's other example regards output redirection.  Trying
to redirect the output (or standard error) of such a
multi-command alias will give surprising results: only the
last command's output will be redirected.

I advise you to use a function (or even a small shell
script) instead of an alias for compound commands like your
examples.  As I hope you can see, the disadvantages are
real.  Worse still, they can be subtle and confusing,
especially to someone unfamiliar with these (not uncommon)
shell features.  The savings of using an alias instead of a
function in order to avoid having to type a few parentheses
and brackets once is a false economy.

good times,
Vineet

Attachment: pgpuhEoLZMrpl.pgp
Description: PGP signature


Reply to: