I have this shell script called 'test.sh' :
#!/usr/bin/env bash
echo "echo 'hello';"
When I run ./test.sh it obviously gives this output:
echo 'hello';
Now this output is also a valid shell statement itself. If I copy & paste this line on the shell and press enter, it says
hello
So far so good.
But I want to do this in one step, so I tried this:
$(./test.sh)
However I now get:
'hello';
In case it matters, I'm using macOS 10.15.6 Catalina which uses the zsh shell by default. I tried the same on Linux using bash, same result.
I also tried $('./test.sh') or $("./test.sh") just in case, but obviously that made no difference.
There's probably some simple explanation and solution to this, but I fail to see it. What am I doing wrong?
(EDIT) Maybe the two echoes are confusing, suppose my test.sh script contains this:
echo "ls *.txt;"
If I now do ./test.sh I'm getting
ls *.txt;
And if I copy and paste this on the shell it shows me all .txt files as expected.
However if I do $(./test.sh) I get:
ls: *.txt;: No such file or directory
What is the right syntax to have whatever the test.sh script outputs, to be executed as a shell command line?