As a user of a shell you have most likely had the need to export a variable to another process; i.e. set/modify an environment variable.
Now, how do you stop exporting an environment variable? can you export anything else?
The bash shell offers the
-n option of the
export built-in, claiming it "
remove[s] the export property". However, this feature is not part of the POSIX:2001 standard and is, therefore, a bashism.
A portable way to stop exporting an environment variable is to
unset it. E.g. the effect of "export MY_VAR=foo" can be reverted by calling "unset MY_VAR" - surely enough, this will also destroy the content of the variable.
An equivalent could then be:
# to stop exporting/"unexport" the MY_VAR environment variable:
my_var="$MY_VAR" ; unset MY_VAR ;
MY_VAR="$my_var" ; unset my_var ;
The above code will make a copy of the variable before destroying it and then restoring its content.
How about exporting other things? did you know that you can
export shell functions?
With the bash shell, you can export a function with the
-f parameter of the
export built-in. Needless to say, this is a bashism. Its secret? it's just an environment variable with the name of the function and the rest of the function definition as its value.
Try this:
$ echo="() { /bin/echo 'have some bash' ; }" bash -c 'echo "Hello World!"'
have some bash
Yes, this means that if you can control the content of an environment variable passed to bash
you can probably execute whatever code you want. It comes handy when you want to alter a script's behaviour without modifying the script itself.
Possibilities are endless thanks to
bash's support for non-standard characters in function names. Functions with slashes can also be exported, for example:
/sbin/ifconfig() {
echo "some people say you should be using ip(1) instead" ;
}
Are you into bug hunting? export exec='() { echo mount this ; }'