2

I have 2 shell scripts called script1.sh (executes on local server ) and script2.sh (executes on remote server which i am passing it to ssh command). and my script1.sh is connecting to unix server and and executing script2.sh as below.

declare RESULT=$(sshpass -p 'password' ssh [email protected] "bash -s" < ./script2.sh )

and script2.sh does some calculations and returns some variable to calling script i.e script1.sh

#!/bin/sh
declare var1="fgte"
return var1

I want to return var1 from script2.sh which is executing on server and should return some value to script1.sh. How can i do that? I need to read the value of var1 in script1.sh. Please help me.

1
  • You can not use return. Use echo. Commented Feb 6, 2017 at 12:10

1 Answer 1

2

The simplest way is to use stdout, and print out the result in your script2.sh (using echo as you've done), and collect it in script1.sh e.g.

#!/bin/sh
result=$(script2.sh $var)

This becomes more complex as/when you return multiple values from a child script, and you have to delineate/parse. For a single value, however, it's a simple and pragmatic approach.

Note this is distinct from the shell script exit code, which is traditionally used to indicate if your script returned successfully or not.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.