export -f
export -f
Posted Oct 2, 2014 14:43 UTC (Thu) by CChittleborough (subscriber, #60775)Parent article: Bash gets shellshocked
Why does Bash parse certain environment variables on startup? Like all POSIX shells, Bash has an export command which puts the specified shell variables into the environment of subsequently-executed commands. But Bash also has a -f option to export which exports shell functions instead of shell variables. For example:
# hello() { echo "Hello, CLI user"; }
# export -f hello
# bash
## hello
Hello, CLI user
## env | grep -A1 Hello,
hello=() { echo Hello, CLI user
}
The other thing going on here is that the Bash parser uses a yacc grammar whose input is specified by static variables. To execute a Bash script file, Bash gets the parser to read from that file then return to its previous source of input. A Bash function is stored as a string and executed by getting the parser to read the string. The code that switches parser input around like this is not all that easy to understand. (I speak from experience.)
Fixing all these bugs is likely to make this code even more complex. For example, exported shell functions now use a new mode in which the parser stops after one command. Sigh.
