Check if a String Contains a Substring in Linux

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 split it into substrings, 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 split it based on colons, 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 to iterate over 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 through 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.

Disabling 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 unsetting IFS, will extract substrings based on the default delimiters (space, tab, and newline).

Alternative Methods

While IFS is useful for word splitting, there are more direct methods for checking substring presence in Linux

# Using grep
echo "Hello World" | grep -q "World" && echo "Found" || echo "Not found"

# Using case statement
string="Hello World"
case "$string" in
  *World*) echo "Substring found" ;;
  *) echo "Substring not found" ;;
esac

# Using parameter expansion
string="Hello World"
if [[ "$string" == *"World"* ]]; then
  echo "Substring found"
fi

Conclusion

In this article we have explored the IFS variable, its default values and how to set custom values in IFS to work with string splitting in Linux. While IFS is primarily designed for word splitting rather than direct substring checking, understanding its behavior helps with text processing and pattern matching tasks. For direct substring checking, methods like grep, case statements, or parameter expansion are more efficient and appropriate.

Updated on: 2026-03-17T09:01:38+05:30

636 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements