It depends on what you mean by return value.
Processes (on UNIX-like systems) can return to a shell a single unsigned byte as an exit status, so that gives a value in the range 0-255. By convention zero means success and any other value indicates a failure.
(In lower-level languages, like C, you can get more than just this exit status, but that's not visible in bash
).
The exit status of the last command run is stored in variable ?
, so you can get its value from $?
, however since many programs only return either 0 (it worked) or 1 (it didn't work), that's not much use.
Bash
conditionals, like if
and while
test for success (exit code of 0) or failure (exit code of non-zero):
if some-command
then
echo "It worked"
else
echo "It didn't work"
fi
However ....
If you mean you want to get the output from the script, that's a different matter. You can capture it using:
var=$(some-command)
But wait, that only captures normal output, routed to a stream called stdout
(file descriptor 1), it does not capture error messages which most programs write to a stream called stderr
(file descriptor 2). To capture errors as well you need to redirect file descriptor 2 to file descriptor 1:
var=$(some-command 2>&1)
The output text is now in variable var
.