Table of Contents
Using Regex Operator with if statement
To check if string starts with another string in bash, use the regex operator =~
with if statement.
1 2 3 4 5 6 7 8 9 |
string="hello world" prefix="hello" if [[ "$string" =~ ^"$prefix" ]]; then echo "The string starts with the prefix." else echo "The string does not start with the prefix." fi |
1 2 3 |
The string starts with the prefix. |
In this script, the =~
operator compares the string
variable to the regular expression. In addition, this operator tests whether the string on the left side matches the regular expression on the right side.
Here, the regular expression ^"$prefix"
matches any string that starts with the $prefix
value. It is because the ^
character anchors the match to the beginning of the string, and the ""
characters ensure that any special characters in the $prefix
are correctly escaped.
If the regular expression matched the beginning of the string, the conditional expression evaluated to true, and the echo
command outputted The string starts with the prefix
. If the regular expression did not match the beginning of the string, the conditional expression was evaluated as false
, and the echo
command outputted The string does not start with the prefix
.
Using if
Statement with == operator
Use an if
statement with == operator to check if a string starts with anoterh string in Bash.
1 2 3 4 5 6 7 8 9 10 |
string="hello world" prefix="hi" if [[ "$string" == "$prefix"* ]]; then echo "The string starts with the prefix." else echo "The string does not start with the prefix." fi |
1 2 3 |
The string does not start with the prefix. |
An if
statement was used in this code to check if the string
variable starts with a prefix
variable or not. If the condition is true, which means the $string
variable begins with the $prefix
variable, the code prints the message The string starts with the prefix
. Otherwise, if the condition is false, the code prints the message The string does not start with the prefix
.
In the above case, the prefix
was not found in the string
; that’s why the script printed the The string does not start with the prefix
message using the echo
command.
Using Double Brackets with equal operator
Use the double brackets ([[...]]
) to check if a string starts with another string in Bash. [[...]]
is a keyword used to create a conditional expression.
1 2 3 4 5 |
string="hello world" prefix="hello" [[ "$string" = "$prefix"* ]] && echo "The string starts with the prefix." || echo "The string does not start with the prefix." |
1 2 3 |
The string starts with the prefix. |
The above code checked whether a string
variable started with a given prefix
variable. For example, the string
variable contained the value hello world
, and the prefix
variable had the value hello
. The comparison was performed using pattern matching with the *
wildcard, which matched any characters following the prefix
.
If the condition is true
, which means the $string
variable starts with the $prefix
variable, the code prints the message The string starts with the prefix
. Otherwise, the code prints the message The string does not start with the prefix
.
In our case, the output was The string starts with the prefix
because the string variable started with the prefix
variable.
Using the case
Statement
To check if a string starts with another string in Bash, use the case
statement with pattern matching.
1 2 3 4 5 6 7 8 9 10 11 12 |
string="The quick brown fox" prefix="The" case "$string" in ("$prefix"*) echo "The string '$string' starts with the prefix '$prefix'." ;; (*) echo "The string '$string' does not start with the prefix '$prefix'." ;; esac |
1 2 3 |
The string 'The quick brown fox' starts with the prefix 'The'. |
In the given code, a bash variable string
was assigned the value The quick brown fox
and another prefix
variable was set as The
.
The code then used the case
statement with the pattern "$prefix"*
to check if the $string
variable starts with the $prefix
variable. If the pattern matches, which means the $string
variable begins with the $prefix
variable, the code prints the message The string 'The quick brown fox' starts with the prefix 'The'.
Otherwise, the code prints the message The string 'The quick brown fox' does not start with the prefix 'The'.
In other words, the code checks whether the string starts with a specific prefix and provides a message indicating whether or not the prefix is present in the string. The case statement is helpful for more complex pattern matching when multiple patterns are needed to be checked.
Note: Using the asterisk symbol (*)
as the final pattern to establish a default case is a widely adopted practice because this pattern will always match.
Using the grep
Command with if statement
Use the grep
command to check if a string starts with another string in Bash.
1 2 3 4 5 6 7 8 9 |
string="apple" prefix="app" if echo "$string" | grep -q "^$prefix"; then echo "The string starts with the prefix." else echo "The string does not start with the prefix." fi |
1 2 3 |
The string starts with the prefix. |
In this example, the echo
command prints the value of the string
variable to grep
. The -q
option is used to make grep
operate in quiet mode, suppressing the output and returning a status code indicating whether a match was found. The regular expression ^$prefix
matches any string that starts with the $prefix
value.
If grep
finds a match, the conditional expression evaluates to true, and the echo command outputs The string starts with the prefix
. If grep
does not find a match, the conditional expression evaluates to false, and the echo command outputs The string does not start with the prefix
.
Note: The grep command can be a powerful tool for pattern matching, especially if you need to search for more complex patterns or use regular expressions.
Further reading:
That’s all about how to check if String starts with another String in Bash.