Checking whether a string contains a substring is a common task in Bash scripting. A string is a group of characters, and a substring is a smaller part of that string. For example, in the string kalilinuxtutorials.com, the word linux is a substring.
This is useful when writing Bash scripts for log analysis, file checking, user input validation, cybersecurity automation, and system monitoring. For example, you may want to check if a log line contains the word Failed, if a URL contains https, or if a filename contains .txt.
The easiest way to check if a string contains a substring in Bash is by using double brackets [[ ]] with wildcard *.
Example:
#!/bin/bashtext="Welcome to Kali Linux Tutorials"if [[ "$text" == *"Linux"* ]]; then echo "Substring found"else echo "Substring not found"fi
Output:
Substring found
In this example, Bash checks whether the word Linux exists inside the variable text.
The basic syntax is:
if [[ "$string" == *"$substring"* ]]; then echo "Found"else echo "Not found"fi
The * symbols act as wildcards. They mean that any text can appear before or after the substring.
You can also check a substring from user input.
#!/bin/bashread -p "Enter a website URL: " urlif [[ "$url" == *"https"* ]]; then echo "The website uses HTTPS"else echo "The website may not use HTTPS"fi
This script asks the user to enter a URL and checks whether it contains https.
Substring checking is also useful when working with file names.
#!/bin/bashfile="report.txt"if [[ "$file" == *".txt"* ]]; then echo "This is a text file"else echo "This is not a text file"fi
This script checks whether the filename contains .txt.
By default, Bash substring matching is case-sensitive.
Example:
#!/bin/bashtext="Linux Security"if [[ "$text" == *"linux"* ]]; then echo "Found"else echo "Not found"fi
Output:
Not found
Here, Linux and linux are treated differently.
To perform a case-insensitive check, convert the string to lowercase:
#!/bin/bashtext="Linux Security"substring="linux"if [[ "${text,,}" == *"${substring,,}"* ]]; then echo "Substring found"else echo "Substring not found"fi The ${text,,} syntax converts the text to lowercase.
Checking if a string contains a substring in Bash is simple and useful. The best method is to use [[ "$string" == *"$substring"* ]]. This method is easy to read and works well in most Bash scripts.
This technique is important for beginners because it helps in input validation, log filtering, file handling, and cybersecurity scripting. Once you understand substring checking, you can write smarter Bash scripts that make decisions based on text patterns.
Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…
Introduction A self-signed SSL certificate is a certificate that is created and signed by the…
Introduction Debugging is an important part of Bash scripting. When a script does not work…
Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…
Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…
Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…