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

Re: aliases versus functions



* alex (radsky@ncia.net) [030606 19:01]:
> I've been using a collection of aliases for a few years and I've 
> been told many times that I should be using 'functions ' to do the 
> same job that my aliases do quite well.  I'm told that aliases are 
> not suited for running compound commands but should be limited to 
> simple equivalents such as
> 
>      alias cd..='cd ..'.   or     alias ls='ls -aF --color=auto'

I think the idea is that functions' syntax is better suited for multiple
commands.  IMO, this is more readable:

win+() {
  mount -t vfat /dev/hda1 /mnt/da1
  cd /mnt/da1
  ls
}

Although, if space is your concern, you could compress it to 

win+() { mount -t vfat /dev/hda1 /mnt/da1; cd /mnt/da1; ls; }

which happens to be fewer characters than the alias line, and doesn't
require you to jump through any extra hoops should your RHS of the alias
include quotes.

Functions allow you to use parameters, simple branching ("return"),
recursion, local variables.  Functions allow you recursion, as well.
The only real reason that this means you should use them instead of
aliases is that there's no real reason not to; you get to use those
features if you want to (at no cost to you if you don't).  If you always
use either one or the other, you don't have to worry about the different
behaviors of how they're expanded and applied.  Since always using
aliases is insufficient, you should use aliases if you always use one or
the other.

> 'Functions' just take up too much space----aliases of compound 
> commands are much more compact and as far as I can tell work as well
> as 'Functions'.  So, why is everyone so set against using aliases 

Well, they don't work as well as functions for all purposes, and I think
I showed that they actually take up less space.  I wouldn't rewrite
working aliases as functions just for the sake of rewriting them,
though.

In your case, if your aliases work for you, great.  "If it ain't
broke...", right?

good times,
Vineet
-- 
http://www.doorstop.net/
-- 
"If you can put it on a T-shirt, it's speech... To enjoin the T-shirts as a
circumvention device is ludicrous." --Robin Gross, EFF staff attorney

Attachment: signature.asc
Description: Digital signature


Reply to: