Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So I started using zsh. I like it all right. It seems very cool and slick, and the fact that the current working directory and actual command line are on different lines is nice, but at the same time, I'm noticing that zsh can be a bit slower than bash, especially when printing text to the screen.

The thing I liked best was the fact that zsh was 'backward compatible' with all of the functions I defined in my .bashrc.

One gripe though. The functions all work perfectly, but I can't figure out how the exporting system works.

I had some of those .bashrc functions exported so that I could use them elsewhere, such as in scripts and external programs, with export -f.

In zsh, exporting doesn't seem to even be talked about. Is it autoloading? Are those two things the same? I'm having a seriously hard time figuring that out.

share|improve this question
This is a very old question, but I want to say that "the current working directory and actual command line are on different lines" has nothing at all to do with zsh. It depends on how you set up your prompt, that's all. – 4ae1e1 May 2 '14 at 17:21
up vote 6 down vote accepted

Environment variables containing functions are a bash hack, I don't think zsh has anything similar. You can do something similar with a few lines of code. Environment variables contain strings; bash stores the function's code in a variable whose name is that of the function and whose value is () { followed by the function's code followed by }.

for v in ${(k)parameters}; do
  [[ "-$parameters[v]-" = *-export-* ]] || continue
  [[ ${(P)v} = '() {'*'}' ]] || continue
  eval "$v ${(P)v}"

I don't recommend doing this. Relying on exported functions in scripts is a bad idea: it creates an invisible dependency in your script. If you ever run your script in an environment that doesn't have your function (on another machine, in a cron job, after changing your shell initialization files, …), your script won't work anymore. Instead, store all your functions in one or more separate files (something like ~/lib/shell/foo.sh) and start your scripts by importing the functions that it uses (. ~/lib/shell/foo.sh). This way, if you modify foo.sh, you can easily search which scripts are relying on it. If you copy a script, you can easily find out which auxiliary files it needs.

Zsh (and ksh before it) makes this more convenient by providing a way to automatically load functions in scripts where they are used. The constraint is that you can only put one function per file. Declare the function as autoloaded, and put the function definition in a file whose name is the name of the function. Put this file in a directory listed in $fpath (which you may configure through the FPATH environment variable). In your script, declare autoloaded functions with autoload -U foo.

share|improve this answer

Your Answer


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.