• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

Bash Exit Command and Exit Code

Written by Admin, Updated On October 1, 2020
bash, terminal
Bash Exit Command and Exit Code

In this article, we will discuss about the Bash exit built-in command and the exit code of the executed commands. Generally, at the time of 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.

Exit Status#

When any shell command terminates, it returns an exit code, even successfully or unsuccessfully.

If the exit code is zero that means command completed successfully and non-zero means an error was encountered.

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

date &> /dev/null
echo $?

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

0

For example, if you try to navigate to nonexisting directory the exit code will be non-zero:

cd /no_dir &> /dev/null
echo $?

You can know the reason to command fail by the status code. You can get the information about the exit codes for each command in man page of each command.

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

If you run the multi-command with pipe, the exit status will be of the last command in the pipeline.

mkdir -p test_dir2 | rm -r test_dir1
echo $?

In the example above echo $? will print the exit code of the rm 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.

Examples#

At the time of writing script, often the exit status of command can be use as condition such as if condition. In below example, grep will exit with zero if the given string found in filename:

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

If you are running a list of commands separated by && or || operators, 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 /work/project && mkdir test

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

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.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article Pgrep Command in Linux
Next Article   Pidof Command in Linux

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook