[Top][All Lists]

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

'official function declaration format' doesn't work if alias defined

From: Linda Walsh
Subject: 'official function declaration format' doesn't work if alias defined
Date: Wed, 06 Jan 2016 15:49:35 -0800
User-agent: Thunderbird

I had an alias referring to printf that I wanted to replace
with a function.

instead of using the function declarator
'function' (or my alias 'sub'), I remembered that the official
way was to use:

 P () {

But then ran into problems with the alias taking precedence over the

Even in the file where the function was defined and exported, I got
an error in the export statement about "P" not being a function.
I also tried unaliasing it:

 unalias P >& /dev/null || ((1))

 export -f P Pe

But still got the "not a function" ... then I realized
I had used the official, POSIX format for function declarations,
above, but guess it had silently been changed into
 printf () {
by the 'alias'.
Sure enough, using the old form:

 function P () {

caused "P" to be defined as a function -- so when it was
unaliased and exported, the export statement found the function.

I am not sure how one would catch a problem like that other than
by make sure the defined function is never the 1st argument on the line
(by adding function).  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?

(bash, version 4.3.39)


reply via email to

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