Shell: How to determine the exit status of Linux and UNIX command
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.
Subscribe to our free e-mail newsletter or RSS feed to get all updates.
You can Email this page to a friend.
Related Other Helpful FAQs:
- What are the exit statuses of ssh command?
- Display Apache Server Status with mod_status
- Shell scripting: read one line at a time from keyboard
- UNIX / Linux Command To Check Existing Groups and Users
- How to: Find Out Whether a UNIX / Linux Process Is Running or Not
Discussion on This FAQ
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: exit status in linux, exit status linux, exit status shell, linux command exit status, linux exit status, unix command status, unix exit status, use command exit status




April 3rd, 2007 at 6:54 pm
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
April 3rd, 2007 at 6:59 pm
G’day again,
Nope, I was wrong the script works I didn’t copy it properly. I left out the #!/bin/bash.
Thanks,
Love your work.
Rob
April 5th, 2008 at 3:55 pm
thank you
it seems very useful for my search
great job
June 1st, 2008 at 10:27 am
Thanks a lot. Useful info and explained well!
July 18th, 2008 at 4:54 am
im a newbie in AIX platform. Just want to ask how to execute this command: #!/bin/bash
August 5th, 2008 (4 weeks ago) at 12:16 pm
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….
August 5th, 2008 (4 weeks ago) at 1:00 pm
Exit status is depend upon program or command. Read man page to find out meaning of return status 1 and 8.
August 20th, 2008 (2 weeks ago) at 8:22 am
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…