Checking exit codes in bash


There are quite a few exit codes used on Linux systems, though no listing you can display when you’re feeling curious. In fact, you won’t see the numeric codes unless you specifically ask for them.

Instead, you will see a textual description of the problem you encountered—such as “No such file or directory”—in a context like this:

$ bin/runme
bash: bin/runme: No such file or directory

If you want to see the numeric exit code, you can use the echo $? command. The error message will tell you that there is no “runme” script in your bin directory. The echo $? command will respond with only a number.

$ echo $?
127

For most commands you run on the Linux command line, the exit code will be 0. This means that no errors or problems were encountered.

$ pwd
/home/justme
$ echo $?
0
$ echo Hello, World
Hello, World
$ echo $?
0

When you try to examine a file that doesn’t exist or misspell the name of a command, you get a larger numeric response.

$ cat nosuchfile
cat: nosuchfile: No such file or directory
$ echo $?
1
$ daet
bash: daet: command not found...
Similar command is: ‘date’
$ echo $?
127

At times, your output might suggest that you’ve not run into any problems, but the error code might still be greater than 1, indicating that some error occurred. You may have to scan through the output to spot a lonely “permission denied” message or two. While I spared myself the screens-full of output that the first command below would have generated, I can still see that it ran into some kind of problem because it left me with an exit code of 1:

$ ls -lR /usr > /dev/null 2>&1
$ echo $?
1

You can also use the bash exit command to exit a shell with a particular exit code. In the commands below, I start a second shell, exit it and then display the exit code that I asked to use.

$ bash			<== start new shell
$ exit 111
exit
$ echo $?
111

You could use any value you want with the exit command but, if you use a value greater than 256, the exit code will be the number you entered minus 256 as shown below. Clearly, fewer than 256 exit codes have been defined.

$ bash
$ exit 257
exit
$ echo $?
1

You can also get a script to exit with a particular code by including an exit command. If you don’t provide a numeric value (i.e., if you use exit with no argument), it will return a 0.

$ cat runme
#!/bin/bash
echo Hello, World
exit 12
$ runme
Hello, World
$ echo $? 12

If you run a script without using the exit command, it will return the exit code resulting from the last command that was run in the script.

$ somescript
hello
cat: nosuchfile: No such file or directory
$ echo $?
1

Keep in mind that any command you run on the command line will return an exit code whether you ask to see it or not. If you ping a system which doesn’t exist on your network or isn’t responding for some reason, you’ll see something like this:

$ ping 192.168.0.111
PING 192.168.0.111 (192.168.0.111) 56(84) bytes of data.
From 192.168.0.7 icmp_seq=1 Destination Host Unreachable
From 192.168.0.7 icmp_seq=2 Destination Host Unreachable
From 192.168.0.7 icmp_seq=3 Destination Host Unreachable
From 192.168.0.7 icmp_seq=4 Destination Host Unreachable
—- 192.168.0.111 ping statistics —-
4 packets transmitted, 0 received, +4 errors, 100% packet loss, time 3061ms
pipe 3
$ echo $?
1

Wrap-Up

Any command that you run on the Linux command line, from the simplest to the most complex, will return an exit code. If you’re ever curious, the echo $? command will inform you of your success (0) or tell you that you encountered some kind of problem (any number greater than 0). This is especially useful when your command generates so much output that you might otherwise not even notice any errors.

Join the Network World communities on Facebook and LinkedIn to comment on topics that are top of mind.

Copyright © 2022 IDG Communications, Inc.



Source link