How does VARIABLE=() { function definition } work in bash

WARNING – this question is about the Bash before the vulnerability, due to which it was changed.


I have seen something like this in my bash ENV:

module=() {  eval `/usr/bin/modulecmd bash $*` }

How does this construct work? What is it called?

I’m not asking about modulecmd, I am asking about the entire construct.

Asked By: mcede

||

It’s really a function named module. It appears in environment variables when you export a function.

$ test() { echo test; }
$ export -f test
$ env | sed -n '/test/{N;p}'
test=() {  echo test
}

From bash documentation – export:

export

 export [-fn] [-p] [name[=value]]

Mark each name to be passed to child processes in the environment. If
the -f option is supplied, the names refer to shell functions
;
otherwise the names refer to shell variables. The -n option means to
no longer mark each name for export. If no names are supplied, or if
the -p option is given, a list of exported names is displayed. The -p
option displays output in a form that may be reused as input. If a
variable name is followed by =value, the value of the variable is set
to value.

The return status is zero unless an invalid option is supplied, one of
the names is not a valid shell variable name, or -f is supplied with a
name that is not a shell function.

Answered By: cuonglm
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.