|
|
Subscribe / Log in / New account

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.


to post comments

export -f

Posted Oct 2, 2014 23:58 UTC (Thu) by giraffedata (guest, #1954) [Link]

Thanks. That really does explain the feature; no other description I've seen of the little-known function-in-environment-variable feature mentions that it's for export -f . What looked like a bizarre feature now looks fairly natural.

It also explains how a Bash change is able to provide the namespace fix (adding a prefix and suffix to the environment variable name when it contains a function): Bash is at both ends of the interface.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds