How to solve the following error without defining `my_test` in `test.sh`?
```bash my_test() { echo test } echo my_test > test.sh bash test.sh ``` ``` test.sh: line 1: my_test: command not found ```
Jul 13, 12:34am
The error occurs because my_test is defined as a shell function in your current shell session, but when you run bash test.sh, it starts a new shell process where my_test is not defined.
Why it fails:
echo my_test > test.sh writes the literal string my_test to test.sh.
When bash test.sh runs, it tries to execute my_test as a command, but it doesn't exist in the new shell.
How to fix it without defining my_test in test.sh:
You need to export the function so it's available in the subshell. Use the export -f command: