O'Reilly logo

The Linux Command Line by William E. Shotts Jr.

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Exit Status

Commands (including the scripts and shell functions we write) issue a value to the system when they terminate, called an exit status. This value, which is an integer in the range of 0 to 255, indicates the success or failure of the command’s execution. By convention, a value of 0 indicates success, and any other value indicates failure. The shell provides a parameter that we can use to examine the exit status. Here we see it in action:

[me@linuxbox ˜]$  ls -d /usr/bin
/usr/bin
[me@linuxbox ˜]$  echo $?
0
[me@linuxbox ˜]$  ls -d /bin/usr
ls: cannot access /bin/usr: No such file or directory
[me@linuxbox ˜]$  echo $?
2

In this example, we execute the ls command twice. The first time, the command executes successfully. If we display the value ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required