0

I'm new in variable concept in bash and I have seen some courses how to use and when to use special variable like $# to see how many arguments were passed in bash script and $@ to see all the arguments supplied to the Bash script.

but I haven't understood $? and they just say that $? (The exit status of the most recently run process) and also I haven't got anything.

I need a little explanation and please give an example.

  • Pretty sure this forum has an answer for that already, Well as far as the shell is concern if the value of `$?` is zero that means it truth and else it is false. So example if the recent command succeeded which means the value of `$?` is zero one can act upon it, either execute another command or do something else. – Jetchisel May 02 '20 at 01:08
  • Could please give an example even if it small – abdulsalam jamaea May 02 '20 at 01:11
  • https://stackoverflow.com/questions/7248031/meaning-of-dollar-question-mark-in-shell-scripts does that answer your question? – Jetchisel May 02 '20 at 01:13
  • 1
    Does this answer your question? [Meaning of $? (dollar question mark) in shell scripts](https://stackoverflow.com/questions/7248031/meaning-of-dollar-question-mark-in-shell-scripts) – 0stone0 May 02 '20 at 01:14
  • @frankmark, I told you, this has already answers in this forum, just need to search first before posting a new question, and if you find that the post in that link is what you're looking for, please vote by clicking the arrow/triangle that is pointing up. – Jetchisel May 02 '20 at 01:16
  • As you mentioned, it gives the exit status of the last executed command. You can check the answer of https://stackoverflow.com/questions/7248031/meaning-of-dollar-question-mark-in-shell-scripts for detailed information with example. Also you can take a look at website below if you are new learner in bash programming. https://devhints.io/bash – Bahadır Çetin May 02 '20 at 01:18

1 Answers1

0

That definition you gave, i.e. the exit status of the most recently run process, is exactly what is is; but if you don't get how processes and commands in general work on the command line, I can understand how that might be a bit confusing.

In general, any command you run on the command line will have an exit code. In something like C/C++, you might have seen it as the return 0; appended at the end of every main() routine, and in shell scripts you might have seen it as exit 0. These return codes are the primary way to signal to the external environment (in this case the terminal) that things either ended well, or they didn't.

This is where $? comes into play. The convention on Unix systems is to have a process return exit code 0 if everything went fine, and return a non-zero value if it didn't. So, let's say I wrote a simple main routine like so:

int main(int argc, char* argv[]) {
    if (argv[1] == "true") {  // not quite right, but fine for examples
        return 0;
    }
    else if (argv[1] == "false") {
        return 1;
    }
}

If I run this in the commnand line as ./a.out true, then this program will return 0 and the $? variable will be set to 0 as well. If I type ./a.out false, however, the program will return 1 and $? will also be set to 1, which indicates that something went wrong.

This seems pretty redundant if you're just toying around in the command line, running commands and then echo $? to see the result, but this really becomes useful in Bash scripting, where you might want to see what the command you just ran returns. For example, the diff command returns 0 if the two files you specify don't differ, and 1 if they do. This gives a neat way to introduce some control flow into your program, but you have to be careful, because $? is set after every command you run.

I hope this helped!

Matthew P.
  • 317
  • 1
  • 5