I understand if I write a bash script I can get the return value, but is there anyway to get the return value without scripting, and just command line?
The return value of a command is stored in the $? variable. The return value is called exit status. This value can be used to determine whether a command completed successfully or unsuccessfully.
$? will give you the last exit code of an executed command/app. You can run your app as normal and then immediately after check what $? has returned and based on that tweak your script logic.
In Linux, run the program at the command prompt and then use the echo command to confirm the return value: $ echo $? At the command prompt in Windows, you must code a batch file to fish out the return value.
return command is used to exit from a shell function. It takes a parameter [N], if N is mentioned then it returns [N] and if N is not mentioned then it returns the status of the last command executed within the function or script. N can only be a numeric value. Syntax: return [N]
Yes, the same way you'd do in a Bash script. Run your program like this:
./your_program; echo $?
In light of the invalidation of the previous answer (good point, Carl Norum), let me re-phrase my comment as an answer:
BASH stores the return value of the previously run command in the variable $?
. This is independent of the programming langauge used to write said command (the command can also be a shell internal).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With