Bash Check if String Starts with Specific Prefix


Bash Check if String Starts with Specific Prefix

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


Syntax

if [[ $string == prefix* ]]; then
    # commands if string starts with prefix
fi

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


Example Bash Check if String Starts with Specific Prefix

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

1. Check if a Variable Starts with a Specific Prefix

This script checks if the variable str starts with the prefix 'Hello' and prints a corresponding message.

#!/bin/bash

str="Hello, World!"
prefix="Hello"

if [[ $str == $prefix* ]]; then
    echo "The string starts with '$prefix'."
else
    echo "The string does not start with '$prefix'."
fi

In this script, the variable str is assigned the value 'Hello, World!', and the variable prefix is assigned the value 'Hello'. The if statement uses the == $prefix* pattern to check if str starts with prefix. If true, it prints a message indicating that the string starts with the prefix. Otherwise, it prints a different message.

Check if a variable starts with a specific prefix in Bash

2. Check if a User Input String Starts with a Specific Prefix

This script prompts the user to enter a string and checks if it starts with the prefix 'Test', then prints a corresponding message.

#!/bin/bash

read -p "Enter a string: " str
prefix="Test"

if [[ $str == $prefix* ]]; then
    echo "The string starts with '$prefix'."
else
    echo "The string does not start with '$prefix'."
fi

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

Check if a user input string starts with a specific prefix in Bash

3. Check if a String from a Command Output Starts with a Specific Prefix

This script checks if the output of a command stored in the variable output starts with the prefix 'dir', then prints a corresponding message.

#!/bin/bash

output=$(ls -d */ 2>/dev/null | head -n 1)
prefix="dir"

if [[ $output == $prefix* ]]; then
    echo "The command output starts with '$prefix'."
else
    echo "The command output does not start with '$prefix'."
fi

In this script, the variable output is assigned the result of a command that lists directories, redirecting any errors to /dev/null, and extracting the first directory. The variable prefix is assigned the value 'dir'. The if statement uses the == $prefix* pattern to check if output starts with prefix. If true, it prints a message indicating that the command output starts with the prefix. Otherwise, it prints a different message.

Check if a string from a command output starts with a specific prefix in Bash

Conclusion

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