0

I came across a bash_profile file that makes use of the export -f statement in the following manner:

# Run xelatex and BibTeX
function xelatex_bibtex {
    xelatex -shell-escape "${1}.tex" &&
        bibtex "${1}" &&
        xelatex -shell-escape "${1}.tex" &&
        xelatex -shell-escape "${1}.tex"
}
export -f xelatex_bibtex

However, functions defined without export -f seem to work perfectly fine:

# Search for synonyms
function syn {
    wn "${1}" -synsn
}

What's the role of export -f and what's considered good practice when creating convenience function in bash_profile with respect to using export?

Konrad
  • 313
  • 1
  • 8

1 Answers1

6

Its role is exactly analogous to that in the case of variables - i.e. to export the definition to inherited environments.

So

$ foo() { echo bar; }
$ foo
bar

Start a child shell

$ bash

Now:

$ foo

Command 'foo' not found, did you mean:

  command 'roo' from snap roo (2.0.3)
  command 'fio' from deb fio
  command 'fgo' from deb fgo
  command 'fog' from deb ruby-fog
  command 'woo' from deb python-woo
  command 'fox' from deb objcryst-fox
  command 'goo' from deb goo
  command 'fop' from deb fop

See 'snap info <snapname>' for additional versions.

whereas the same child shell after exporting the function:

$ export -f foo
$ bash
$ foo
bar
steeldriver
  • 78,509
  • 12
  • 109
  • 152