Shell: How to determine the exit status of Linux and UNIX command

Posted on in Categories , , , , , , , , , , , last updated June 1, 2008

Q. Can you explain the exit status of shell and commands under Linux / UNIX operating system?

A. All UNIX and Linux command has a several parameters or variables that can be use to find out the exit status of command. Please note that these parameters or variables may only be referenced assignment to them is not allowed. You can use $? to find out the exit status of command. $? always expands to the status of the most recently executed foreground command or pipeline. For example, you run the command cal:
$ cal
Now to see exit status of cal command type following command:
$ echo $?
Output:

0

Zero means command executed successfully, if exit status returns non-zero value then your command failed to execute. For example run command called cyberciti
$ cyberciti
Output:

bash: cyberciti: command not found

Display exit status of the command:
$ echo $?
Output:

127

Value 127 (non-zero) indicates command cyberciti failed to execute. You can use exit status in shell scripting too. You can store result of exit status in variable. Consider following shell script:

#!/bin/bash
echo -n "Enter user name : "
read USR
cut -d: -f1 /etc/passwd | grep "$USR" > /dev/null
OUT=$?
if [ $OUT -eq 0 ];then
   echo "User account found!"
else
   echo "User account does not exists in /etc/passwd file!"
fi

Save and execute the script as follows:
$ chmod +x script.sh
$ ./script.sh

Output:

Enter user name : jradmin
User account does not exists in /etc/passwd file

Try it one more time:
$ ./script.sh
Output:

Enter user name : vivek
User account found

As you can see, I have used grep command to find out user name stored in USR variable. If grep command finds user name in /etc/passwd command output it would return exit status of zero. This is stored in OUT variable. Next, if command makes decision based upon exit status stored in OUT variable.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

18 comment

  1. Mate, I followed your script and it says “can’t execute binaries?

    Don’t know seem pretty good, but doesn’t quite get there for me.
    I am running Ubuntu Dapper Drake Linux.

    Keep it coming, it is good stuff.

    Rob

  2. $ ./cyberciti; echo $?
    -bash: ./cyberciti: No such file or directory
    127

    $ ssh localhost “./cyberciti; echo $?”
    bash: ./cyberciti: No such file or directory
    0

    ???

    1. Maybe Chris will see this, or maybe not. Anyway…

      The reason you get an exit code of “0” on that last one is that ssh was successful in connecting to localhost and executing your command. The command itself failed. But ssh worked. So, ssh returns zero in that instance. Exit codes don’t call back from internal commands. Look at this for instance:
      $ ls filenotfound.txt; echo $?
      ls: cannot access filenotfound.txt…
      2
      $ ls filenotfound.txt &
      ls: cannot access filenotfound.txt…
      echo $?
      0
      [2]+ Exit 2 ls filenotfound.txt

      Same thing happens when you fork off a process. You successfully submitted the job, so you get a zero exit code.

  3. The script has a bug: If your acount is “vivek” and you type “viv” that say you “User account found”, you can solve it with this:
    […]
    cut -d: -f1 /etc/passwd | grep “^””$USR””$” > /dev/null
    […]

  4. Hi,
    I need to know the return values which we get after executing the run command in UNIX.
    I know only 0 for successful. What are the other values present.
    Also, could anyone tell me about the description of those errors.

    Thanks,
    Thayananth

Leave a Comment