ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The command $? means
return value of the last command executed.
Being the case, the script below should display a number
2 twice on the console because of the exit $? command.
"return value" is not the same as "output".
The return value (better: exit code" is an 'invisible' number passed to the shell from a program that ends. Often this is used to indicate the program ecountered an error: 0 when the program exited normally, and some other number (1..128) if there was an error.
But your script is also a program, so it also returns an exit code (or: return value). You can do:
exit 1
exit 100
exit 0
In your script, echo $n will probably run fine, and thus returns an exit code of 0 to your script. Then you $? will be "0". So your script returns exit code 0 to the schell where your script was started from.
This can be useful: to return the same exit code as the last command before "exit" returned, But from your post I understand this was not what you expected.
"exit $?" does not run the last command again. Where did you get this idea from? Novell login scripts maybe?
You asked
"exit $?" does not run the last command again. Where did you get this idea from? Novell login scripts maybe?"
On page 136 of the book "The UNIX Programming Environment" by Brian W. Kernighan and Bob Pike, it states that
"$? - return value of the last command executed."
Originally posted by Linh On page 136 of the book "The UNIX Programming Environment" by Brian W. Kernighan and Bob Pike, it states that
"$? - return value of the last command executed."
Exactly. This does not mean, it will execute it another time. It's just the return value / exit code of a command already executed.
By using the script exit $? how would I know what code it returned to shell ? How would I print out the exit code returned to the shell ? Would it be something like echo exit $?
The are return codes - the status of the process or file when it exited.
Generally 0 means good, any other number means error.
Code:
cat /usr/include/sysexits.h
shows you how exit codes are used. 128 - 172 are "reserved" for the system or shell, for example. Most code just uses a 1 for every error, which is not very informative.
Hi jim mcnamara. Thank you for your help.
When I used the Bash command exit $? as shown below, it did not print out a 0. It did echo a 2, so there is no error, but it did not print out a 0 on the console. How do I get exit $? to print a number returned to the shell on the console ?
=======================
#!/bin/bash -p
n=2
echo $n
exit $?
======================
In the code below, I issued an illegal command "aaa", but it did print a returned error code on the console. How do I know what error number was returned to the shell ?
root:/home/usr-suid-apache# ./a
./a: aaa: command not found
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.