what is the zsh equivalent of bash's export -f

ixtmixilix

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.

Gilles 'SO- stop being evil'

Environment variables containing functions are a bash hack. Zsh doesn't have anything similar. You can do something similar with a few lines of code. Environment variables contain strings; older versions of bash, before Shellshock was discovered, stored 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 }. You can use the following code to import variables with this encoding, and attempt to run them with bash-like settings. Note that zsh cannot emulate all bash features, all you can do is get a bit closer (e.g. to make $foo split the value and expand wildcards, and make arrays 0-based).

bash_function_preamble='
    emulate -LR ksh
'
for name in ${(k)parameters}; do
  [[ "-$parameters[name]-" = *-export-* ]] || continue
  [[ ${(P)name} = '() {'*'}' ]] || continue
  ((! $+builtins[$name])) || continue
  functions[$name]=$bash_function_preamble${${${(P)name}#"() {"}%"}"}
done

(As Stéphane Chazelas, the original discoverer of Shellshock, noted, an earlier version of this answer could execute arbitrary code at this point if the function definition was malformed. This version doesn't, but of course as soon as you execute any command, it could be a function imported from the environment.)

Post-Shellshock versions of bash encode functions in the environment using invalid variable names (e.g. BASH_FUNC_myfunc%%). This makes them harder to parse reliably as zsh doesn't provide an interface to extract such variable names from the environment.

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.

Furthermore zsh can compile scripts, to save parsing time. Call zcompile to compile a script. This creates a file with the .zwc extension. If this file is present then autoload will load the compiled file instead of the source code. You can use the zrecompile function to (re)compile all the function definitions in a directory.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Local variables in zsh: what is the equivalent of bash's "export -n" in zsh

From Dev

Local variables in zsh: what is the equivalent of bash's "export -n" in zsh

From Dev

What is the zsh equivalent of bash's "command" command?

From Dev

What's the ZSH equivalent of BASH's $PROMPT_COMMAND?

From Dev

What is the bash equivalent of zsh vared command?

From Dev

bash's command number ( \# ) equivalent in zsh

From Dev

zsh and POSIX equivalent of bash's `{var}>&1`

From Dev

What is the ZSH equivalent of 'expr'?

From Dev

zsh: What glob expression is equivalent to `find . -type f`?

From Dev

function's calling context in zsh: equivalent of bash `caller`

From Dev

Zsh equivalent of bash's `expr index "abcdefghijklmnopqrstuvwxyz" xyzd`

From Dev

what's the powershell equivalent to Bourne/Bash -d and -f when testing if a string is a folder or a file?

From Dev

What is the bash equivalent of Python's all?

From Dev

What is the PowerShell equivalent of bash's exec()?

From Dev

What is the equivalent of bash's !$ and !! in the fish shell?

From Dev

What's the Bash equivalent of "cmd /c"

From Dev

What is the equivalent of Bash's cat -n in PowerShell?

From Dev

What is the bash equivalent of DOS's pause command?

From Dev

What's the equivalent to && when writing a bash script?

From Dev

What exactly is <() in bash (and =() in zsh)?

From Dev

What's the Windows command shell equivalent of Bash's `true` command?

From Dev

zsh equivalent of bash show-all-if-ambiguous?

From Dev

What, if any, is the equivalent of bash's select...in...do..done in fish?

From Dev

What's the equivalent of bash parentheses at the command line in powershell?

From Dev

ZSH alternative to BASH export VAR+='...'

From Dev

What are the equivalent F# bitwise operators to OCaml's 'lsr' and 'asr'?

From Dev

In LINQPad , what is the equivalent of C#'s .Dump() method in F#?

From Dev

What does "$-" mean in bash or zsh?

From Dev

What is the 'export GST_DEBUG=<#>' equivalent for Android?

Related Related

  1. 1

    Local variables in zsh: what is the equivalent of bash's "export -n" in zsh

  2. 2

    Local variables in zsh: what is the equivalent of bash's "export -n" in zsh

  3. 3

    What is the zsh equivalent of bash's "command" command?

  4. 4

    What's the ZSH equivalent of BASH's $PROMPT_COMMAND?

  5. 5

    What is the bash equivalent of zsh vared command?

  6. 6

    bash's command number ( \# ) equivalent in zsh

  7. 7

    zsh and POSIX equivalent of bash's `{var}>&1`

  8. 8

    What is the ZSH equivalent of 'expr'?

  9. 9

    zsh: What glob expression is equivalent to `find . -type f`?

  10. 10

    function's calling context in zsh: equivalent of bash `caller`

  11. 11

    Zsh equivalent of bash's `expr index "abcdefghijklmnopqrstuvwxyz" xyzd`

  12. 12

    what's the powershell equivalent to Bourne/Bash -d and -f when testing if a string is a folder or a file?

  13. 13

    What is the bash equivalent of Python's all?

  14. 14

    What is the PowerShell equivalent of bash's exec()?

  15. 15

    What is the equivalent of bash's !$ and !! in the fish shell?

  16. 16

    What's the Bash equivalent of "cmd /c"

  17. 17

    What is the equivalent of Bash's cat -n in PowerShell?

  18. 18

    What is the bash equivalent of DOS's pause command?

  19. 19

    What's the equivalent to && when writing a bash script?

  20. 20

    What exactly is <() in bash (and =() in zsh)?

  21. 21

    What's the Windows command shell equivalent of Bash's `true` command?

  22. 22

    zsh equivalent of bash show-all-if-ambiguous?

  23. 23

    What, if any, is the equivalent of bash's select...in...do..done in fish?

  24. 24

    What's the equivalent of bash parentheses at the command line in powershell?

  25. 25

    ZSH alternative to BASH export VAR+='...'

  26. 26

    What are the equivalent F# bitwise operators to OCaml's 'lsr' and 'asr'?

  27. 27

    In LINQPad , what is the equivalent of C#'s .Dump() method in F#?

  28. 28

    What does "$-" mean in bash or zsh?

  29. 29

    What is the 'export GST_DEBUG=<#>' equivalent for Android?

HotTag

Archive