Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Bash Exit Command and Exit Codes - DesignLinux

designlinux 0 Comments

Often when writing Bash scripts, you will need to terminate the script when a certain condition is met or to take action based on the exit code of a command.

In this article, we will cover the Bash exit built-in command and the exit statuses of the executed commands.

Exit Status #

Each shell command returns an exit code when it terminates, either successfully or unsuccessfully.

By convention, an exit code of zero indicates that the command completed successfully, and non-zero means that an error was encountered.

The special variable $? returns the exit status of the last executed command:

date &> /dev/nullecho $?

The date command completed successfully, and the exit code is zero:

0

If you try to run ls on a nonexisting directory the exit code will be non-zero:

ls /nonexisting_dir &> /dev/nullecho $?
2

The status code can be used to find out why the command failed. Each command’s man page includes information about the exit codes.

When executing a multi-command pipeline, the exit status of the pipeline is that of the last command:

sudo tcpdump -n -l | tee file.outecho $?

In the example above echo $? will print the exit code of the tee command.

Bash exit command #

The exit command exits the shell with a status of N. It has the following syntax:

exit N

If N is not given, the exit status code is that of the last executed command.

When used in shell scripts, the value supplied as an argument to the exit command is returned to the shell as an exit code.

Examples #

The commands’ exit status can be used in conditional commands such as if. In the following example grep will exit with zero (which means true in shell scripting) if the “search-string” is found in filename:

if grep -q "search-string" filename then
  echo "String found."
else
  echo "String not found."
fi

When running a list of commands separated by && (AND) or || (OR), the exit status of the command determines whether the next command in the list will be executed. Here, the mkdir command will be executed only if cd returns zero:

cd /opt/code && mkdir project

If a script ends with exit without specifying a parameter, the script exit code is that of the last command executed in the script.

~/script.sh
#!/bin/bash

echo "doing stuff..."

exit

Using just exit is the same as exit $? or omitting the exit.

Here is an example showing how to terminate the script if invoked by non-root user:

#!/bin/bash

if [[ "$(whoami)" != root ]]; then
  echo "Only user root can run this script."
  exit 1
fi

echo "doing stuff..."

exit 0

If you run the script as root, the exit code will be zero. Otherwise, the script will exit with status 1.

Conclusion #

Each shell command returns an exit code when it terminates. The exit command is used to exit a shell with a given status.

If you have any questions or feedback, feel free to leave a comment.

bash terminal

Related

Tags: bash, terminal

A Beginners Guide to Snaps in Linux – Part 1

Prev Post

How to Install GCC on Ubuntu 20.04

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 562
  • 604,524

DesignLinux.com © All rights reserved

Go to mobile version