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

Re: Functions or aliases?



On 2004-05-09 21:12:56 -0400, alex wrote:
> 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?

If you add arguments or something else (a redirection or a & for
instance) after the alias in the command line, this will apply
to the last command.

Here's another example:

ay:~> alias foo='ls -R > bar; date'
ay:~> foo &

This is equivalent to

  ls -R > bar
  date &

not

  { ls -R > bar; date } &

(This is the zsh syntax, I don't know about the other shells.)

> 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?

Well, here an analogy with mathematics. You can write c = a + b
and you consider c * d. If you regard "c = a + b" as a string
replacement, then c * d would mean a + b * d; this is how aliases
work. If you regard "c = a + b" as an expression definition or an
equality (the normal case in math, but this should also be the
case in Unix shells), then c * d means (a + b) * d.

Aliases are just string replacements. Functions are much more
structured (very similar to shell scripts).

In a shell, if I type "foo", this is just like a command and I also
expect that "foo &" would start something in the background. Ditto
for "foo > file": I expect all the output of foo to be redirected
to file.

-- 
Vincent Lefèvre <vincent@vinc17.org> - Web: <http://www.vinc17.org/>
100% validated (X)HTML - Acorn / RISC OS / ARM, free software, YP17,
Championnat International des Jeux Mathématiques et Logiques, etc.
Work: CR INRIA - computer arithmetic / SPACES project at LORIA



Reply to: