[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: embraced redefinition of aliases and function causes endless recursi
From: |
Al Elgert |
Subject: |
Re: embraced redefinition of aliases and function causes endless recursion |
Date: |
Mon, 15 Apr 2002 19:21:02 +0200 |
User-agent: |
Mutt/1.3.25i |
Hello
On Mon, Apr 15, 2002 at 12:17:47PM -0400, Chet Ramey wrote:
> > Machine Type: sparc-sun-solaris2.7
> >
> > Bash Version: 2.05a
> > Patch Level: 0
> > Release Status: release
> >
> > Description:
> > Consider an alias is calling a function and the function
> > calls an executable, function or bultin.
> > (The alias is used to wrap only the interactive call of the
> > function.)
> > Then bash goes into endless recursion, if the alias is declared
> > before and after the function OR the function is declared before
> > and after the alias.
> > A embraced redefinition always happens, if the .bashrc is
> > sourced again.
> >
> > Repeat-By:
> > alias cd=cda
> > function cda() { cd "$@"; }
> > alias cd=cda
> > cd /
>
> You've created a recursive function. Function definitions themselves are
> compound commands. Alias expansion is performed while parsing the function
I see, that makes the difference:
$ alias cd=cda
$ function cda() { cd "$@"; }
$ type cda
cda is a function
cda ()
{
cda "$@"
}
$
> definition. You end up with this function:
>
> function cda() { cda "$@"; }
>
> The implications should be clear.
>
> You can change things to do what you want by using the `builtin' command
> in the function definition. You don't need the alias at all, in fact.
>
> cd() { builtin cd "$@"; }
Consider the problem you want to have "another" cd in your shell functions
as in your "hand-typed-command-line" for example:
function cd_function() {
local fstype
builtin cd "$@"
fstype=$(find -maxdepth 0 -mindepth 0 -type d -printf "%F\n")
if [ "$cd_fstype" != "$fstype" ]; then
echo "$cd_fstype -->> $fstype"
fi
cd_fstype="$fstype"
}
alias cd=cd_function
Now you can call cd from any function without the output from "cd_function".
But the cd from command line will notify you, if you change the filesystem.
(Well, it would be better to use "builin cd" in any function.)
> You could also quote the cd in the function definition.
Thanks, it was a little bit confusing, but now it is clear.
Alexander
--
Alexander Elgert
Public Gruppe
RechnerBetriebsGruppe TU Darmstadt (FB 20) Tel: +49 06151 16-4333
Raum: S1/13 11a (alt 25/11a)