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:
bashecho 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.
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:
bashcat << 'EOF' > test.sh my_test() { echo test } my_test EOF bash test.sh
cat << 'EOF' > test.sh writes everything between EOF to test.sh.my_test is properly defined in the file.my_test is called at the end of the file, so it executes when you run bash test.sh.texttest
If you want to write the function directly from the command line:
bashecho 'my_test() { echo test; }' > test.sh echo 'my_test' >> test.sh bash test.sh
This will also work and produce the same output.
Vibe can make mistakes. Check answers. Learn more