'How to use shell functions in a script
I have a shell function that I am trying to call from my zsh script but it is not finding the function. I assume this is because the script has no context to the environment? How do I get the script to utilize the shell functions?
$ function foo { echo bar }
$ foo
bar
In my my_script.zsh file I have:
#!/usr/bin/zsh
echo Foo
foo
But when I run this I get:
$ ./my_script.zsh
Foo
./my_script.zsh:4: command not found: foo
Is there any way I can use my shell functions inside of my scirpt?
Solution 1:[1]
You have not defined the function. While bash would have so-called exported functions, with which you could achieve the desired effect (though I don't consider it wise to do so), zsh doesn't. A common approach is to put the function definitions in a separated file (say, funlib.zsh), and source this file from every zsh process which needs them, i.e. something like
source ~/lib/funlib.zsh
(or wherever you put your function definitions).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user1934428 |
