0

Since macOS 10.15 (Catalina) the default shell has changed from bash to zsh. One of the things I'm running into is I cannot get my own global functions working. I used to export these from .bash_profile but zsh doesn't seem to know the concept of exporting functions.

Say I have this:

function greet { echo "Hello $1, how are you today" }

If I then run hello RocketNuts on the shell, it says Hello RocketNuts, how are you today. So far so good.

Now I want to make this function global so that it's also available in scripts.

I have tried:

  • putting it in .zshrc
  • putting it in .zshenv

I also tried creating a subdir ~/myfunctions and a file called ~/myfunctions/greet which contains:

function greet { echo "Hello $1, how are you today" }
greet "$@"

and then in either ~/.zshrc or ~/.zshenv I add:

fpath=( ~/myfunctions "${fpath[@]}" )
autoload -Uz greet

However, none of these methods make the greet function available in scripts.

From the shell, they all work fine. With either method, I can invoke the greet function manually on the shell.

But if I have a file test.sh which does greet Somebody and run that, it always says "greet: command not found".

How do I get this working in zsh?

7
  • Your forgot to post the content of greet.sh. Apr 12 '20 at 17:27
  • @Gilles'SO-stopbeingevil' They have ~/myfunctions/greet there. Does it have to have a .sh filename suffix?
    – Kusalananda
    Apr 12 '20 at 17:32
  • @Kusalananda The suffix is not relevant, but the full content of the file is. Possibly even the way the script is executed. But at the very least the file contents. Apr 12 '20 at 17:35
  • @Gilles'SO-stopbeingevil' it's there in my post, after "a file called ~/myfunctions/greet which contains:" (the contents are just those 2 lines).
    – RocketNuts
    Apr 12 '20 at 17:59
  • Oh ok. So that's not a zsh script: without a shebang line, it's an sh script. That's why I asked for the whole file. Apr 12 '20 at 18:06