We learned how to launch a program from command line [Refer: https://www.thebookofc.com/environment/launching-from-command-line/]. But how do we check the return value of a program executed on command line?

How to check return value on Linux?

To know the return value of a program executed from the command line, all we need to do is type the following command on command prompt immediately after the program is executed:

user@server:~$ echo $?

Let’s understand it with an example. Following code adds up all the digits passed on command line and returns the sum.

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char * argv[]) {
    int sum = 0;
    for (int i=0; i<argc; i++){
        sum += atoi(argv[i]);
    }
    return sum;
}

We compile the C-file to make a binary file named ‘sum’:

user@server:~$ gcc sum.c -o sum

Let’s execute it from command line and see the results:

user@server:~$ ./sum 10 23 11
user@server:~$ echo $?
44

What if I execute the binary twice? Which result will be displayed? Let’s check:

user@server:~$ ./sum 1 2 3
user@server:~$ ./sum 4 5 6
user@server:~$ echo $?
15

As you can see, the command ‘echo $?’ displays the return status of last executed command.

System commands in Linux too have a return value. If any system command is executed successfully, the return status is ZERO. If it is any other value, then there was some error in execution. e.g. let us try to execute a file with no execute permission:

user@server:~$ ls -l sum.c
-rw-rw-r– 1 user user 181 Dec 26 12:36 sum.c
user@server:~$ echo $?
0

user@server:~$ ./sum.c
bash: ./sum.c: Permission denied
user@server:~$ echo $?
126

In above sequence, the command ls was executed successfully, hence its return value was printed as zero. However when we try to execute a file which has no execute permissions, the return value is non-zero.

On bash shell

  • 127 is returned for the reason ‘file not found’
  • 126 is returned if file doesn’t have execute permissions.
  • 128 + signal-number if program was terminated by a signal

How to check return value on Windows?

On windows the counterpart of $? is %errorlevel%. We can check the return value of last executed system command or user binary by typing following on command prompt:

C:\Users\priyanka>echo %errorlevel%

For user created binaries, the return value of program is printed.

For system commands, a value zero indicates successful execution, whereas a non-zero value is indicator of some error in execution.

C:\Users\priyanka\Desktop>dir mute.png
Volume in drive C is OS
Volume Serial Number is F4C7-BD78

Directory of C:\Users\priyanka\Desktop

16-10-2018 12:20 329,800 mute.png
1 File(s) 329,800 bytes
0 Dir(s) 879,569,223,680 bytes free

C:\Users\priyanka\Desktop>echo %errorlevel%
0

Here the system command dir, was successfully executed and return value hence is zero.

C:\Users\priyanka\Desktop>mute
‘mute’ is not recognized as an internal or external command,
operable program or batch file.

C:\Users\priyanka\Desktop>echo %errorlevel%
9009

As you can see, in above example a non-zero value is returned in case of error.