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?
-
7Exactly the same way as in a script.Mat– Mat2011-11-22 16:36:07 +00:00Commented Nov 22, 2011 at 16:36
-
Thanks Mat I got it working. As I'm sure you can tell I'm pretty new to developing in linux.Brandon Yates– Brandon Yates2011-11-22 16:42:48 +00:00Commented Nov 22, 2011 at 16:42
Add a comment
|
2 Answers
Yes, the same way you'd do in a Bash script. Run your program like this:
./your_program; echo $?
4 Comments
gspr
To point out the essence of this answer: BASH stores the return value of the previously run command in the variable
$?.Carl Norum
-1. The
&& means you'll only ever get a 0 response - echo won't get run unless your_program succeeds.Carl Norum
What? The whole point is to find the exit status of the program, right? Your code only gets the exit status on success. If you already know it succeeded, why bother checking? Anyway, your update is fine - downvote removed.
jweyrich
@Carl: ahhhh, ok. I see your point now, and you're right. Thanks! :)
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).