To illustrate my issue and questions consider two scripts.
Script1.sh
#!/bin/sh
test=1
echo $test
Script2.sh
#!/bin/sh
$(Script1.sh)
When I run Script1.sh from the command line directly via ./Script1.sh it produces the desired output.
1
However when I run Script2.sh which calls Script1.sh, instead I get the following error message:
Script2.sh: line 1: 1: command not found
It thinks that the integer 1 is a command for some odd reason?
I found a cheap work around which is to set a variable from the first script and pass it to the second script as a parameter but I would like to understand why this is failing.
Also, when I changed my variables value from 1 to "some text", I would expect that my echo would print "some text" to the screen. But when I call Script2.sh (which ultimately just calls Script1.sh) it doesn't print the echo to the screen. Why is that, and how do I resolve it so I can see the echo output the same as if I would have called Script1.sh directly?
Any advice would be greatly appreciated.
You're calling it wrong by using command substitution. Just use source or a subshell if you want it:
. ./Script1.sh
( ./Script1.sh )
The second one is a bit redundant since it will always run in another shell anyway, so this would make more sense instead:
( . ./Script1.sh )