```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 you are writing the function definition to the file test.sh as a string, not as executable Bash code. When you run:
bash
echo my_test > test.sh
You are literally writing the text my_test into test.sh, not the function definition. So, when you run bash test.sh, it tries to execute my_test as a command, which doesn't exist.
How to Fix It
To write the function to test.sh and make it executable, you need to write the entire function definition to the file. Here's how: