DragonFly BSD
DragonFly users List (threaded) for 2004-11
[Date Prev][Date Next]  [Thread Prev][Thread Next]  [Date Index][Thread Index]

Re: Aliases versus scripts


From: Oliver Fromme <olli@xxxxxxxxxxxxxxxxxx>
Date: 24 Nov 2004 16:34:06 GMT

Jonathon McKitrick <jcm@xxxxxxxxxxxxxxxxx> wrote:
 > just a general question here.  When you perform a task manually often, like
 > cvsupping, do you find it better to write a script or just set up an alias
 > to avoid typing a long command repeatedly?

Short advice:

If it's a single command that you don't use in any other
context, an alias is probably most appropriate.

Long story:

The most important difference is that aliases only work in
your login shell.  You cannot call them in scripts or any-
where else where a program (other than your login shell)
executes a command.  For example, aliases don't work with
xargs.

A small example:  I once had an alias "grip" which was just
a short-cut for "grep -i".  However, it did not work with
xargs.  Since "find ... | xargs grep ..." is a very useful
combination, I changed that "grip" alias into a small shell
script, so it works with xargs.

Another advantage of shell scripts is that they're more
portable.  Well, at least if they're written in a portable
way.  You can just copy them to a different system and they
will work, independently of your login shell.

The disadvantage is that shell scripts take a little more
resources than aliases.  While an alias just takes those
bytes necessary to store it in your login script, a shell
script will at least take one inode, one directory entry
and one fragment (2 Kbytes by default).  Well, you can put
several commands into one shell script and use links to
save diskspace (using "case ... esac" on $0).  Apart from
that, disk space is pretty cheap these days, and 2 Kbytes
is nothing.

Another disadvantage of aliases (beside the above-mentioned
one) is that they're very limited.  This depends on your
login shell.  For example, in zsh, aliases aren't much more
than simple string replacement.  They don't take arguments,
and they're unsuitable for longer constructs which would
require multiple lines.  They are, however, suitable for
very simple replacement things.  Here are a few favourites
(in zsh syntax):

   alias ..='cd ..'
   alias \?='echo `date` `pwd`'
   alias l='ls -l'
   alias L='ls -al'
   alias beep="printf '\\a'"

Beside aliases and scripts, there's a third possibility:
shell functions.  They're defined in your login script, so
they have the same scope as aliases (i.e. they don't work
with xargs etc.).  _But_ they are a lot more flexible than
aliases, because they can take arguments and can contain
arbitrarily amounts of shell code, i.e. multiple lines.
Just a simple example from my ~/.zshrc:

   SetFlag()
   {
           if [[ "x$2" == "x-R" || "x$2" == "x-r" ]]; then
                   set -- -R "$1" "${@[3,-1]}"
           fi
           chflags "$@"
   }

   schg()   { SetFlag schg   "$@"; }
   noschg() { SetFlag noschg "$@"; }

Now I can type "noschg /kernel" or "schg -R /modules".

There's one clever thing where aliases are extremely useful:
You can use aliases to intercept wildcard expansion, because
alias expansion happens before anything else (except histo-
ry expansion).

For example, I've had a shell script named "calc" that could
be used for mathematical calculations on the command line
(among other things), i.e. I could type "calc 4 \* 3" and
it would answer "12".  However, the asterisk ("*") always
needed to be escaped or quoted, otherwise the shell would
expanded it to all filenames in the current directory.
So I renamed the script to "calc.sh" and created an alias:

   alias calc="noglob calc.sh"

Now i can type "calc 4 * 3" without having to escape / quote
the "*", and it does the right thing.  (That last feature
might be zsh-specific.)

Best regards
   Oliver  (whose ~/.zshrc is 16 Kbytes / 600 lines)

-- 
Oliver Fromme, Konrad-Celtis-Str. 72, 81369 Munich, Germany

``All that we see or seem is just a dream within a dream.''
(E. A. Poe)



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