1. Introduction

Case sensitivity plays a crucial role in the Linux operating system (OS) for maintaining system security, whether working with files, executing commands, or writing a script. For example, file.txt and File.txt are considered different despite having the same name.

In this tutorial, we’ll discuss various methods to check a string for uppercase ASCII letters in Bash, using regular expression, parameter expansion, grep, awk, and sed commands.

2. Using Regular Expression

Regular expressions or regex are commonly used for string manipulation. They enable us to define and search for specific sequences of characters within the text for pattern matching, such as finding all uppercase letters:

$ if [[ "Welcome to Baeldung" =~ [A-Z] ]]; then
  echo "String contains an uppercase letter"
else
  echo "String does not contain an uppercase letter"
fi
String contains an uppercase letter.

Here, we used the if conditional statement for matching a pre-defined string Welcome to Baeldung. Next, we used the regex pattern [A-Z] with the =~ operator. This operator checks if the string on its left side matches the pattern on the right.

In this case, if it finds any uppercase letters, the echo command prints String contains an uppercase letter. Otherwise, it prints the String does not contain an uppercase letter.

A better practice is to define the string as a separate variable, especially if the string is long:

$ var="Welcome to Baeldung"

$ if [[ $var =~ [A-Z] ]]; then
  echo "String contains an uppercase letter."
else
  echo "String does not contain an uppercase letter."
fi
String contains an uppercase letter.

So, we used a variable var containing Welcome to Baeldung. Next, we interpolated var with $ to get its value. The rest of the code is the same as in the first example and produces the same output.

Importantly, we’ll use this var variable throughout this article.

3. Using Parameter Expansion

We can use parameter expansion to modify variable values or to extract specific information from them. It simplifies variable handling and enhances the versatility of our scripts.

This feature isn’t only helpful for tasks like extracting or replacing text in a string but also for checking for uppercase letters:

$ if [[ ${var} != ${var,,} ]]; then
    echo "The string contains uppercase letters."
else
    echo "The string contains only lowercase letters."
fi
The string contains uppercase letters.

Here, we compare ${var} with ${var,,}. The ${var} variable holds the original string, while the ${var,,} is the parameter expansion that converts the original string to lowercase. We compare these two variables using the not equal  != comparison operator.

Hence, if the string in variable var does contain any uppercase letters, then the condition becomes true; otherwise, it’s false.

4. Using the grep Command

The grep command is primarily used to search for specific words or patterns in a string that match a specified search criteria.

We can utilize the grep command to find the uppercase letters by combining them with regex:

if echo "${var}" | grep -q '[A-Z]'; then
    echo "The string contains uppercase letters."
else
    echo "The string does not contain uppercase letters."
fi
The string contains uppercase letters.

In this script, the pipe (|) operator takes the output of the preceding echo command and uses it as input for the grep command. In particular, the regex pattern [A-Z] matches uppercase letters from A to Z. Next, the -q option executes the process quietly without printing the matching characters and prints the desired output based on the conditions.

5. Using the awk Command

awk is a powerful command for text processing and data manipulation. We can mainly use it for searching or extracting specific information from a string, including uppercase letters:

if echo "${var}" | awk '/[A-Z]/ {exit 0}'; then
    echo "The string contains uppercase letters."
else
    echo "The string does not contain uppercase letters."
fi
The string contains uppercase letters.

Here, the awk command searches for uppercase letters in var using the‘/[A-Z]/’ pattern. When the pattern matches any uppercase letter, the ‘{exit 0}’ quits the processing.; otherwise, it runs the false condition.

6. Using the sed Command

The sed command is a text-processing utility that can transform text in various ways. In this case, we’re using the sed command to identify uppercase letters within a string:

$ if echo "${var}" | sed '/[A-Z]/q'; then
    echo "The string contains uppercase letters."
else
    echo "The string does not contain uppercase letters."
fi
The string contains uppercase letters.

Here, we used the sed command to search for uppercase letters by matching the /[A-Z]/ pattern. When it locates any uppercase letter, the q command inside the sed statement instructs it to quit processing. As a result, the true condition is executed. Otherwise, it executes the false condition.

7. Conclusion

In this article, we’ve discussed multiple methods to check uppercase letters in a string. The parameter expansion and regular expression provide a concise and efficient solution for such tasks. Although the grep, awk, and sed commands can also do this job, they are usually more complicated but provide a wider range of capabilities.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.