Bourne Shell Exit Status Examples For Linux and Unix

Can you explains and provide us “Bourne Shell Exit Status Code” examples?

Each UNIX command returns a status when it terminates. If it is not successful, it returns a code which tells the shell to print an error message. You can use the
exit command to leave a shell program with a certain exit status.

Typical Values Of Exit Status On Bourne Shell

  • 0 – Success.
  • 1 – A built-in command failure.
  • 2 – A syntax error has occurred.
  • 3 – Signal received that is not trapped.

How do I print exit status when using bourne shell?

Type the command:
$ ls
To print its exit status type the command:
$ echo $?
Try a few more examples:
date1
echo $?
date
echo $?
cat /etc/passwd
echo $?
grep vivek /etc/passwd
echo $?

Bourne Shell Exit Status Examples For Unix Command

Linux or Unix exit code tutorial for /bin/sh with examples

How to store or use exit codes in scripts

To store exit status of the last executed command to a shell variable called status, enter:
command1
status=$?

You can use exit status with test command or if command too.

#!/bin/sh
user="$1"
if grep "$user" /etc/passwd; then
        echo "$user has an account"
else
        echo "$user doesn't have an account"
fi

Run it as follows:
./script-name vivek
The grep command is used as condition but it can actually be any command. If it returns a zero exit status, the condition is true; otherwise, it is false. In this example, the while loop executes given commands as long as condition is true. Again, condition can be any command, and is true if the command exits with a zero exit status.

while condition; do
        commands
done

Here is another simple example:

#!/bin/sh
x=0
while [ $x != 3 ]
do
     let x=x+1
    echo $x
done

Here is another example:

#!/bin/sh
while [ -r "$1" ]
do
    cat $1 >> output
    shift
done

Run it as follows:
./script-name file1 file2 fil3
cat output

How to set an exit code value for my own script

To set an exit code in your script use:
exit N
exit 0
exit 999

Conclusion

This page showed how to get exit status of command when using Bourne shell (sh). For more info see:


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

🐧 1 comment 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
1 comment… add one

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