There are fairly a couple of exit codes used on Linux methods, although no itemizing you may show while you’re feeling curious. Actually, you gained’t see the numeric codes except you particularly ask for them.
As an alternative, you will notice a textual description of the issue you encountered—resembling “No such file or listing”—in a context like this:
$ bin/runme bash: bin/runme: No such file or listing
If you wish to see the numeric exit code, you should utilize the echo $? command. The error message will let you know that there is no such thing as a “runme” script in your bin listing. The echo $? command will reply with solely a quantity.
$ echo $? 127
For many instructions you run on the Linux command line, the exit code might be 0. Which means that no errors or issues have been encountered.
$ pwd /house/justme $ echo $? 0 $ echo Hiya, World Hiya, World $ echo $? 0
Once you attempt to look at a file that doesn’t exist or misspell the identify of a command, you get a bigger numeric response.
$ cat nosuchfile cat: nosuchfile: No such file or listing $ echo $? 1 $ daet bash: daet: command not discovered... Comparable command is: ‘date’ $ echo $? 127
At occasions, your output would possibly counsel that you just’ve not run into any issues, however the error code would possibly nonetheless be higher than 1, indicating that some error occurred. You might have to scan by means of the output to identify a lonely “permission denied” message or two. Whereas I spared myself the screens-full of output that the primary command beneath would have generated, I can nonetheless see that it bumped into some form of downside as a result of it left me with an exit code of 1:
$ ls -lR /usr > /dev/null 2>&1 $ echo $? 1
You can too use the bash exit command to exit a shell with a selected exit code. Within the instructions beneath, I begin a second shell, exit it after which show the exit code that I requested to make use of.
$ bash <== begin new shell $ exit 111 exit $ echo $? 111
You can use any worth you need with the exit command however, should you use a worth higher than 256, the exit code would be the quantity you entered minus 256 as proven beneath. Clearly, fewer than 256 exit codes have been outlined.
$ bash $ exit 257 exit $ echo $? 1
You can too get a script to exit with a selected code by together with an exit command. In case you don’t present a numeric worth (i.e., should you use exit with no argument), it should return a 0.
$ cat runme #!/bin/bash echo Hiya, World exit 12 $ runme Hiya, World
$ echo $? 12
In case you run a script with out utilizing the exit command, it should return the exit code ensuing from the final command that was run within the script.
$ somescript hiya cat: nosuchfile: No such file or listing $ echo $? 1
Remember the fact that any command you run on the command line will return an exit code whether or not you ask to see it or not. In case you ping a system which doesn’t exist in your community or isn’t responding for some purpose, you’ll see one thing like this:
$ ping 192.168.0.111
PING 192.168.0.111 (192.168.0.111) 56(84) bytes of knowledge.
From 192.168.0.7 icmp_seq=1 Vacation spot Host Unreachable
From 192.168.0.7 icmp_seq=2 Vacation spot Host Unreachable
From 192.168.0.7 icmp_seq=3 Vacation spot Host Unreachable
From 192.168.0.7 icmp_seq=4 Vacation spot Host Unreachable
—- 192.168.0.111 ping statistics —-
4 packets transmitted, 0 obtained, +4 errors, 100% packet loss, time 3061ms
pipe 3
$ echo $?
1
Wrap-Up
Any command that you just run on the Linux command line, from the only to probably the most advanced, will return an exit code. In case you’re ever curious, the echo $? command will inform you of your success (0) or let you know that you just encountered some form of downside (any quantity higher than 0). That is particularly helpful when your command generates a lot output that you just would possibly in any other case not even discover any errors.
Copyright © 2022 IDG Communications, Inc.