Check if a String Contains a Substring in Linux


Introduction

Working with strings in Linux can be a bit tricky, but with the right tools it can be a piece of cake. A common task many Linux users have to perform is to check if a string contains a specific substring. This can be done using a variety of methods, including regular expressions, string manipulation commands, and programming languages ​​like Python or Perl. However, in this article, we will explore one of the most popular and efficient methods to check if a string contains a substring in Linux, that is by using the special shell variable IFS (Internal Field Separator). We will discuss the default values ​​of IFS variables, understand their various use cases, and explore some implementations that set custom values ​​in IFS.

IFS variable and its default values

The special shell variable IFS determines how Bash recognizes word boundaries when splitting a sequence of character strings. IFS defaults to a three-character string consisting of a space, a tab, and a newline. This can be checked using the following command −

$ echo "$IFS" | cat -et

The output of this command will be −

^I$

“^I” indicates a tab stop and $ indicates a newline. This means that, by default, Bash will split a string based on spaces, tabs, and newlines. For example, if we have a string "foo bar foobar" and we want to check if it contains the substring "bar", we can use the following command −

string="foo bar foobar"
for i in $string
do
  echo "'$i' is the substring"
done

The output of this command will be −

'foo' is the substring
'bar' is the substring
'foobar' is the substring

This demonstrates that the default space value of the IFS variable is used to split the input string into individual substrings.

IFS and Word Splitting

The IFS variable plays a crucial role in word splitting, which is the process of splitting a string into separate words based on the values ​​of the IFS variable. The default space, tab, and newline values ​​for the IFS variable make it easy to split strings based on these characters. However, we can also set custom values ​​in the IFS variable to split strings based on other characters.

Setting Custom Values in IFS

Besides using the default IFS values, we can also set custom values ​​in the IFS variable. This helps us deal with multiple field-delimited strings. For example, if we have a string "foo:bar:foobar" and we want to check if it contains the substring "bar", we can use the following command −

string="foo:bar:foobar"
old_ifs="$IFS"
IFS=":"
for i in $string
do
  echo "'$i' is the splitted word"
done
IFS="$old_ifs"

The output of this command will be −

'foo' is the splitted word
'bar' is the splitted word
'foobar' is the splitted word

This demonstrates that by setting the custom value of ":" to IFS, we were able to split the input string into individual substrings based on the ":" delimiter.

IFS and Substring Check

By understanding the behavior of IFS and setting custom values ​​in it, we can check if a string contains a specific substring. We can use the for loop and loop on the input string and check if the current substring is equal to the target substring. If so, we know that the target substring is present in the input string.

For example, suppose you have a string "Hello world" and you want to check if it contains the substring "World". We can use the following command −

string="Hello World"
IFS=" "
for i in $string
do
   if [ $i == "World" ]; then
      echo "The substring 'World' is present in the string 'Hello World'."
   fi
done

This command iterates the input string, splits it into individual substrings based on the space delimiter, and checks if the current substring is equal to the target substring "World". If so, it prints the message that the substring is present in the input string. Disarm IFS

Another important thing to consider is that we can also disable IFS to restore the default behavior of Bash. For example, if we have a string "foo bar foo:bar" and we set the IFS variable to ":", we can disable IFS to reset it to its default value and split the input string by spaces, tabs and newlines.

string="foo bar foo:bar"
IFS=":"
for i in $string; do echo "[$i] extracted"; done
unset IFS
for i in $string; do echo "[$i] extracted"; done

The first for loop will extract substrings based on the ":" delimiter, while the second for loop, after disabling IFS, will extract substrings based on the default delimiters (space, tab, and newline).

Conclusion

In this article we have explored the IFS variable, its default values ​​and how to set custom values ​​in IFS to check if a string contains a substring. The IFS variable is a powerful tool for text processing and pattern matching. By understanding its behavior, we can use it to check if a string contains a specific substring, which can be a useful task in many Linux operations. Remember, always test your commands before doing this.

Updated on: 20-Jan-2023

354 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements