Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

The shellshock bug in bash works by way of environment variables. Honestly I was suprised by the fact that there is such a feature like:

"passing on of function definitions via env vars"

Therefore this question while maybe not perfectly formulated is to ask for an example or a case in which it would be necessary to have this feature?

Bonus. Do other shells zsh, dash etc. also have this feature?

share|improve this question
    
It uses the env vars to pass on function definitions. What do you mean with "Why does it not simply keep the environment variables available/accessible?" ? –  Anthon 2 days ago
    
@Anthon Thanks for the comment. Maybe I should be clearer and rephrase to. For what reason is it necessary to be able to pass on function definitions via env vars? –  humanityANDpeace 2 days ago
    
I'm not 100% sure but I think that that e.g. is the way that GNU parallel get function definitions distributed if it invokes multiple bash instances. If not in that way it would have to write those in a file, that each invoked instance reads in and then you have to deal with problems like when that file can be removed. –  Anthon 2 days ago
2  
this thread has some history to it. exported functions, though, are insane. if you want a new shell to inherit old shell executable commands you .dot source the same file the old shell did. thats how its done - and that makes sense - or you feed the new shell the file as input when execing. once its read in once the file is cached by the kernel anyway. –  mikeserv 2 days ago
    
@mikeserv Do I understand it right, that a big part of the shellshock is related to this feature which is not that much essential anyways? –  humanityANDpeace 23 hours ago

1 Answer 1

up vote 2 down vote accepted

When a script invokes another script, variables of the parent script can be exported, and then they'll be visible in the child script. Exporting functions is an obvious generalization: export the function from the parent, make it visible in the child.

The environment is the only convenient way a process can pass arbitrary data to its children. The data has to be marshalled into strings that don't contain null bytes, which isn't a difficulty for shell functions. There are other potential methods, such as shared memory blocks or temporary files passed via file descriptors, but these could cause problems with intermediate programs that don't know what to do with them or would close them. Programs expect to run in an environment that contains variables that they don't know or care about, so they won't go overwriting or erasing them.

The choice of using the function name as the name of the environment variable is a strange one. For one thing, it means that an exported variable clashes with an exported function of the same name.

Exported functions are an old feature. Functions were added in the Bourne shell in SVR2, and exported functions in the Version 8 shell released the same year (1984). In that shell, variables and functions used the same namespace. I don't know how function export worked. The Heirloom shell is based on a Bourne variant which has functions but doesn't export them.

ATT ksh supposedly supports exporting functions, but looking at the source or playing with it, I can't see that it does, as of ksh93u.

env -i /usr/bin/ksh -c 'f=variable; f () { echo function; }; typeset -fx f; /usr/bin/env; ksh -c f'
_=*25182*/usr/bin/env
PWD=/home/gilles
SHLVL=1
A__z="*SHLVL
ksh: f: not found

Ksh's public domain clones (pdksh, mksh), dash and zsh don't support exporting functions.

share|improve this answer
    
thank you! So, without exporting functions feature functionality is lost, yet there are some shell like dash, zsh and pdksh etc. which lack this funcitonality, do I read this right? –  humanityANDpeace 23 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.