Peter da Silva writes:
> On 2008-01-04, at 13:50, Aaron Crane wrote:
>> function rename_function {
>> local old=$1 new=$2
>> eval "$(declare -f $old | sed "1s/^$old/$new/")"
>> unset -f $old
>> }
>
>
> Neat trick!
>
> I would make that "1s/^$old /$new /" myself,
Yes. The first version I wrote used Perl to do the munging, and used
\b in the regex to achieve the same effect.
> and quote "$old" and "$1" and so on...
It's not necessary to quote a variable on the right-hand side of an
assignment:
$ f() { local x=$1; echo "[$x]"; }
$ f 'foo bar'
[foo bar]
Also, I decided I didn't care about any brokenness resulting from
however the user might have managed to persuade the shell to create a
function with a non-identifier-syntax name. After all:
$ foo\ bar() { echo blah; }
-bash: `foo\ bar': not a valid identifier
$ function foo\ bar { echo blah; }
-bash: `foo\ bar': not a valid identifier
--
Aaron Crane