How to Exit the Bash Script if a Certain Condition Occurs?

Bash scripts are typically executed sequentially, meaning each command is executed one after the other. Sometimes, while the Bash is running, the developers set the condition that the script must end when a certain condition is met. This action is taken to serve the purpose instantly instead of waiting for the overall execution of the script. 

Keeping this in view, this post offers different methods to exit the bash script when a certain condition is met.

  • Using the exit Command
  • Using the return Statement
  • Using the kill Command
  • Using The killall Command

Method 1: Using the exit Command

The simplest way to exit a Bash script is by using the exit command. This command terminates the script and returns an exit status code to the shell, as shown in the example below.

#!/bin/bash
filename="file.txt"
if [ ! -f "$filename" ]; then
  echo "File does not exist"
  exit 1
fi
echo "File exists"

Code Explanation

  • The code will check for the file with the name “file.txt.”
  • If it does not exist, a message will be printed on the terminal, and the script will be exited using an exit status code of 1. 
  • If the file exists, the script will continue to execute, and a message will be printed on the terminal.

The output of the code can be seen by executing the bash script:

$ bash exit_script.sh

The file used in the bash script is “file.txt,” which does not exist. That is why the script exited with the message printed on the terminal.

Method 2: Using the return Statement

The return statement is used in functions to return a value when the function is being called. However, it can also be used to exit a script when a specific condition is met, and one of its examples is mentioned below:

#!/bin/bash
check_file() {
  filename="$1"
  if [ ! -f "$filename" ]; then
    echo "File does not exist"
    return 1
  fi
  echo "File exists"
  return 0
}
check_file "file.txt"

Code Explanation

  • A function has been defined with the name of check_file that takes a filename as an argument. 
  • A message will be displayed on the terminal and return an exit status code of 1 if the file does not exist. 
  • If the file exists, a message will be printed on the terminal with an exit status code of 0. 
  • The check_file function will be called with a filename of file.txt.

The output of the code can be seen by executing the bash script:

$ bash exit_script.sh

Method 3: Using the kill Command

The kill command is typically used to send a signal to a process; however, it can also be used to exit a script when a specific condition is met; this can be seen below in the example.

#!/bin/bash
while true; do
  read -p "Enter a number: " num
  if [ "$num" -eq 0 ]; then
    echo "Exiting script"
    kill $$
  fi
  echo "You entered: $num"
done

Code Explanation

  • An infinite loop that reads a number from the user using the while true statement. 
  • If the input is 0, a message will be printed on the terminal and exit the script by using the kill command to send a signal to the current process (represented by $$).

The output of the code can be seen by executing the bash script as shown below:

$ bash exit_script.sh

Method 4: The killall Command

The killall command is used to send a signal to all processes with a specific name and can also be used to exit a script when a specific condition is met, as shown in the example below.

#!/bin/bash
while true; do
  read -p "Enter a number: " num
  if [ "$num" -eq 0 ]; then
    echo "Exiting script"
    killall bash
  fi
  echo "You entered: $num"
done

Code Explanation

The explanation of the above code is mentioned below.

  • An infinite loop has been created using a “while true” condition that reads a number from the user. 
  • If the input is 0, we print a message to the console and exit the script by using the killall command to send a signal to all Bash processes.

Conclusion

A bash script generally executes commands one after the other, which is a time taking process, especially if the script is too long. So if a user is looking for any specific command to execute in the entire script and then later wants to exit the script. Then this could be done by following this article as four major methods have been discussed, which are the “exit“, “kill“, and “killall” commands, along with the use of return statements.