Bash – Check if string starts with lowercase alphabet
To check if string starts with a lowercase alphabet in Bash scripting, you can use regular expression ^[a-z](.*)$
. In this expression ^
matches starting of the string, [a-z]
matches a lowercase alphabet, (.*)
matches any other characters, and $
matches end of the string.
Examples
In the following script, we take a string in str
which starts a lowercase alphabet. We shall programmatically check if string str
starts with a lowercase alphabet using regular expression.
example.sh
</>
Copy
#!/bin/bash
str="apple@123"
if [[ $str =~ ^[a-z](.*)$ ]]; then
echo "String starts with lowercase."
else
echo "String does not start with lowercase."
fi
Output
sh-3.2# ./example.sh
String starts with lowercase.
Now let us take a value in the string str
such that it does not start with a lowercase alphabet, but with a digit.
example.sh
</>
Copy
#!/bin/bash
str="3apple@123"
if [[ $str =~ ^[a-z](.*)$ ]]; then
echo "String starts with lowercase."
else
echo "String does not start with lowercase."
fi
Output
sh-3.2# ./example.sh
String does not start with lowercase.
References
Conclusion
In this Bash Tutorial, we learned how to check if string starts with a lowercase alphabet using regular expression.