Bash Check if String Ends with Specific Suffix


Bash Check if String Ends with Specific Suffix

In Bash scripting, checking if a string ends with a specific suffix is useful for various string validation and conditional tasks.


Syntax

if [[ $string == *suffix ]]; then
    # commands if string ends with suffix
fi

The basic syntax involves using the == *suffix pattern within a double-bracket [[ ]] if statement to check if the string ends with the specified suffix.


Example Bash Check if String Ends with Specific Suffix

Let's look at some examples of how to check if a string ends with a specific suffix in Bash:

1. Check if a Variable Ends with a Specific Suffix

This script checks if the variable str ends with the suffix '.txt' and prints a corresponding message.

#!/bin/bash

str="example.txt"
suffix=".txt"

if [[ $str == *$suffix ]]; then
    echo "The string ends with '$suffix'."
else
    echo "The string does not end with '$suffix'."
fi

In this script, the variable str is assigned the value 'example.txt', and the variable suffix is assigned the value '.txt'. The if statement uses the == *$suffix pattern to check if str ends with suffix. If true, it prints a message indicating that the string ends with the suffix. Otherwise, it prints a different message.

Check if a variable ends with a specific suffix in Bash

2. Check if a User Input String Ends with a Specific Suffix

This script prompts the user to enter a string and checks if it ends with the suffix '.log', then prints a corresponding message.

#!/bin/bash

read -p "Enter a string: " str
suffix=".log"

if [[ $str == *$suffix ]]; then
    echo "The string ends with '$suffix'."
else
    echo "The string does not end with '$suffix'."
fi

In this script, the user is prompted to enter a string, which is stored in the variable str, and the variable suffix is assigned the value '.log'. The if statement uses the == *$suffix pattern to check if str ends with suffix. If true, it prints a message indicating that the string ends with the suffix. Otherwise, it prints a different message.

Check if a user input string ends with a specific suffix in Bash

3. Check if a String from a Command Output Ends with a Specific Suffix

This script checks if the output of a command stored in the variable output ends with the suffix '.conf', then prints a corresponding message.

#!/bin/bash

output=$(ls /etc/*.conf 2>/dev/null | head -n 1)
suffix=".conf"

if [[ $output == *$suffix ]]; then
    echo "The command output ends with '$suffix'."
else
    echo "The command output does not end with '$suffix'."
fi

In this script, the variable output is assigned the result of a command that lists configuration files in the /etc directory, redirecting any errors to /dev/null, and extracting the first file. The variable suffix is assigned the value '.conf'. The if statement uses the == *$suffix pattern to check if output ends with suffix. If true, it prints a message indicating that the command output ends with the suffix. Otherwise, it prints a different message.

Check if a string from a command output ends with a specific suffix in Bash

Conclusion

Checking if a string ends with a specific suffix in Bash is a fundamental task for string validation and conditional operations in shell scripting. Understanding how to check for suffixes can help you manage and validate strings effectively in your scripts.