Unset bash function variable with non-standard name

I can have this function in a bash script that gets into the shell

function suman{ echo "using suman function" } 

if i call

 unset suman 

everything works as expected

however, if I have this as my function:

 function suman-inspect { echo "using suman-inspect function" } 

then if i call

 unset suman-inspect 

or

 unset "suman-inspect" 

I get this message:

 bash: unset: `suman-inspect': not a valid identifier 

How can I disable this variable as is?

+6
source share
2 answers

After several studies, it turned out that

 unset -f "suman-inspect" 

will work. This is surprising because unset suman really worked, and successfully canceled the suman function (as far as I could tell).

+4
source

Bash allows you to use functions with names that are not valid identifiers that must be created when they are not in posix mode.

So:

 set -o posix function suman-inspect { echo "using suman-inspect function"; } 

gives:

 bash: `suman-inspect': not a valid identifier 

ruakh makes a valid point by quoting man bash . The source code ( builtins/set.def ) has a comment: Posix.2 first indicates the try parameters, then the functions, but ...

the POSIX standard says that if neither -f or -v is specified, the name refers to a variable; if a variable with this name does not exist, it is not known whether a function with this name, if any, should not be set.

So the actual behavior is that the old backup is "unspecified." Anyway, the error is contained in the bash documentation. But to be honest, in another place man bash says:

Function definitions can be removed using the -f option for undefined inline.

+2
source

Source: https://habr.com/ru/post/1012662/


All Articles