The grep tool is used to search and match the specified Patterns in a file or string. While searching and matching the upper case and lower case letters differ and do not match the upper case pattern with the lower case string. This is called case sensitivity. But the case sensitivity can be disabled and grep can search case insensitive in a string.
Grep Case Sensitive
The default behavior of the grep command is case sensitive. Case sensitive accepts the lower case different from uppercase. For example, the pattern “LINUX” doesn’t matches with the “linux” or “Linux” or “LinuX” etc. The text file is like below.
Linux LINUX LinuX L1nux 1LiNuX
We will use the following grep command which is case sensitive by default.
grep "LINUX" file.txt
The output is like below.
Grep Case Insensitive with -i Option
The case insensitive search can be made with the -i option for the grep command. The search pattern “LINUX” matches with “Linux”, “LiNux” or “LinuX” for the case insensitive search.
grep -i "LINUX" file.txt
The out is like below.
Linux LINUX LinuX 1LiNuX
The –ignore-case is the long-form version of the -i option. So we can use the –ignore-case for case insensitive match with the grep command too.
grep --ignore-case "LINUX" file.txt
Grep Case Insensitive with Other Command Output
Alternatively, another command output can be also grepped with the grep command. This command output can be grepped in a case insensitive manner by using the -i option. In the following example, we will grep case insensitive the cat command output. We will also use the pipe operator to redirect cat command output to the grep command.
cat file.txt | grep -i "LINUX"
Grep Permanent Case Insensitive Configuration
The case insensitive math with the grep command can be made permanent. Even there are different ways the most practical and easy way is using a bash alias. We will create a bash alias named “grep” which refers to the “grep -i”. Every time the grep command is called the case insensitive option added by default. We will just add the alias like below.
echo "alias grep=grep -i" >> ~/.bashrc
If you want to make a case-sensitive search with this permanent case insensitive configuration you can use the –no-ignore-case option which makes the match is case sensitive.
grep --no-ignore-case "LINUX" file.txt