Passing error message back to script

I have one script that calls another script during execution. The other script does some processing, then either returns with exit 0 (if successful), or exits with error code numbers (if failed). However, in addition to the error code, I would like for that second script to be able to pass a message, in a string perhaps, to the initial script.

Would that be feasible?

You can better returning the numbers only and use mappings with number and error description to recognize it.

May be with command substitution.

Hint:

lem@biggy:/tmp$ cat file1
#!/bin/sh
false || echo "problem"
exit 0

lem@biggy:/tmp$ cat file2
#!/bin/sh
echo first
echo $(/tmp/file1)
echo last
exit 0

lem@biggy:/tmp$ ./file2
first
problem
last
lem@biggy:/tmp$
1 Like

Simply source the called script, then all variables set there are available in the calling script too.

$ ./script1.sh
calling script 2
String: it works
Value: 1

$ cat script1.sh
echo calling script 2
. ./script2.sh
RET_VAL=$?
echo String: $RET_STRING
echo Value: $RET_VAL

$ cat script2.sh
RET_STRING="it works"
return 1
$
1 Like

Or ... read the output from the called script:

#script1
./script2 | read reply
echo "Reply is: ${reply}"

#script2
echo "Working"
exit 0

./script1
Reply is: Working
1 Like