13

Looking at the output of env, I noticed that the following function is also listed.

BASH_FUNC_mc%%=() {  . /usr/share/mc/mc-wrapper.sh }

The content of the /usr/share/mc/mc-wrapper.sh file is the following.

MC_USER=`id | sed 's/[^(]*(//;s/).*//'`
MC_PWD_FILE="${TMPDIR-/tmp}/mc-$MC_USER/mc.pwd.$$"
/usr/bin/mc -P "$MC_PWD_FILE" "$@"

if test -r "$MC_PWD_FILE"; then
        MC_PWD="`cat "$MC_PWD_FILE"`"
        if test -n "$MC_PWD" && test -d "$MC_PWD"; then
                cd "$MC_PWD"
        fi
        unset MC_PWD
fi

rm -f "$MC_PWD_FILE"
unset MC_PWD_FILE

What do the %% characters mean in the function name? Do they make it the function invoked in specific cases, or do they allow me to call it differently from other functions?

If it makes any difference, I am using OpenSUSE 42.3, with Bash version 4.3.42(1)-release (x86_64-suse-linux-gnu).

2 Answers 2

16

The function name was crafted by bash updated as a response to the shellshock vulnerability.

There was a function named mc that was exported and your bash version is renaming it by prepending BASH_FUNC_ and replacing () by %%.

$ d() { date ; }
$ export -f d
$ env | grep %%
BASH_FUNC_d%% { date

Here is the bash patch by Florian Weimer that introduced this fix, dated Sept 25 2014:

http://seclists.org/oss-sec/2014/q3/att-693/variables-affix.patch

Note that a function name can contain almost any characters in bash just like a command name in general (i.e. a file name) so %% is definitely valid here.

0
3

It seems that bash is quite happy to use % characters in function names:

bash$ TEST%%() { echo test; }
bash$ TEST%%
test

whereas e.g. dash doesn't like them:

$ TEST%%() { echo test; }
dash: 1: Syntax error: Bad function name

So as far as I can tell, %% doesn't have any special meaning in a bash function name. It would be just like using XX instead. This is despite the definition of a name in the manpage:

   name   A word consisting only of  alphanumeric  characters  and  under-
          scores,  and beginning with an alphabetic character or an under-
          score.  Also referred to as an identifier.
2
  • But there is an equal = sign there after %%! Which causes assignment there. Oct 29, 2017 at 8:40
  • 3
    That is just how env printed the function definition.
    – Wodin
    Oct 29, 2017 at 9:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .