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 $?


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

bash: cyberciti: command not found

Display exit status of the command:
$ echo $?


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:

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

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


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

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

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.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 18 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
18 comments… add one
  • Rob Apr 3, 2007 @ 18:54

    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 Apr 3, 2007 @ 18:59

    G’day again,

    Nope, I was wrong the script works I didn’t copy it properly. I left out the #!/bin/bash.


    Love your work.


  • moheb Apr 5, 2008 @ 15:55

    thank you

    it seems very useful for my search
    great job

  • powerpleb Jun 1, 2008 @ 10:27

    Thanks a lot. Useful info and explained well!

  • mike Jul 18, 2008 @ 4:54

    im a newbie in AIX platform. Just want to ask how to execute this command: #!/bin/bash

  • Poonam Aug 5, 2008 @ 12:16

    Can you tell me what ist he difference between return status of 1 and 8. I tries to search but could not find it on web….

  • 🐧 nixCraft Aug 5, 2008 @ 13:00

    Exit status is depend upon program or command. Read man page to find out meaning of return status 1 and 8.

  • Poonam Aug 20, 2008 @ 8:22

    I tried to find out, but did not find anything. We are using bourne shell….If in my shell script I am exiting with return status of 1 or 8, what difference will it make…

  • chris neale Jul 17, 2010 @ 16:52

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

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


    • tuh Jul 12, 2011 @ 17:45

      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…
      $ ls filenotfound.txt &
      ls: cannot access filenotfound.txt…
      echo $?
      [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.

      • Erik Feb 15, 2016 @ 14:40

        $ ssh localhost “./cyberciti; echo $?” Did you notice it was all in quotes?

  • M P Nov 9, 2010 @ 13:23

    Very helpful. Thanks very much.

  • saravanakumar Jun 12, 2011 @ 14:44

    Very useful…thanks a lot…its makes me to understand about “$?”.

  • wjuarezq Oct 27, 2011 @ 8:11

    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

  • Toby Ho Feb 6, 2012 @ 4:15

    Thanks! That helped.

  • Thayananth Sep 20, 2012 @ 11:23

    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.


  • Rahul Jawale Dec 17, 2012 @ 11:55

    You are doing good work. Thank you!!

  • davb Mar 6, 2013 @ 16:05

    Actually, rather than grep “^””$USR””$”, just use grep -w $USR ….easy!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum