[Top][All Lists]

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

Re: aliases v. functions; command extension? (-f & -F)?

From: Linda Walsh
Subject: Re: aliases v. functions; command extension? (-f & -F)?
Date: Fri, 08 Jan 2016 12:14:01 -0800
User-agent: Thunderbird

For what it's worth, It might be useful to have something like
'command' that (or maybe a flag to 'command', like "-f & -F" that would
search for functions for the 'command' either before ("-f")
the PATH search, or after ("-F"), for the case of the command not
being found in the PATH.

Reuti wrote:
 Seems like using 'function' is safer.

Perhaps the idea of leaving off "function" at the beginning of a function
isn't such a good practice?

Even if you use it: someone could define an alias named function - and this 
stands for all commands.
   My scripts are designed to assist.  I rarely need to design them to be
"invulnerable" against malicious actions. I usually define an alias or, when
I want "elaboration", a function, that uses '$(type -P cmd)' as the path
(elaboration = things like handling error conditions or running command as
In some cases it might be possible to check the exit code of `alias P` resp. `type P` 
beforehand, or to remove with "unalias -a" all aliases.
"unalias P 2>&- || ((1))" seemed best for this case.

"unalias -a" would usually not be the best thing to do, in my environment, as aliases are usually more helpful than not. There are several places in my shell
scripts where functions don't work (like in this case).  Seems like the best
solution, usually, is to define your own alias in the script and use that. The down-side of that is if you later want to introduce a function in place of the
alias -- I guess _that_'s a good reason to use an include file like:

 "include stdalias"

so all of one's aliases are in one place. Although that requires that an 'include' command be available and defined going into a script (I use a
function to define that in my system "/etc/bash_env").


Greg Wooledge wrote:
Yup. That's one of the many evils of aliases. They need to be removed
(unalias) before you can redefine them as functions.
Not if you include a prefix indicating it is a function (e.g. "function").
It was my trying to use 'recommended' format that got me in trouble in this

I usually define my own alias, "sub", for function. Besides being shorter, it also would seem to protect against this type of problem and is another case
where a function can't do the job of an alias.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]