Bash - Return value from subscript to parent script

I have two Bash scripts. The parent scripts calls the subscript to perform some actions and return a value. How can I return a value from the subscript to the parent script? Adding a return in the subscript and catching the value in the parent did not work.

Answers


I am assuming these scripts are running in two different processes, i.e. you are not "sourcing" one of them.

It depends on what you want to return. If you wish only to return an exit code between 0 and 255 then:

# Child (for example: 'child_script')
exit 42

# Parent
child_script
retn_code=$?

If you wish to return a text string, then you will have to do that through stdout (or a file). There are several ways of capturing that, the simplest is:

# Child (for example: 'child_script')
echo "some text value"

# Parent
retn_value=$(child_script)

Need Your Help

MPMoviePlayerController : a way to get used bandwidth?

iphone mpmovieplayercontroller bandwidth

I would like to check used bandwidth when playing a video with MPMoviePlayerController to be able to play a video which matched client bandwidth.