Most people are familiar with using grep to print lines that match a string from a file. But, what if you want to grep a string in a variable? If you pass a variable as an argument to grep, you will get an error (or several if your variable contains spaces).

[savona@putor ~]$ I="This is a variable set on the command line"
[savona@putor ~]$ grep a $I
grep: This: No such file or directory
grep: is: No such file or directory
grep: a: No such file or directory
grep: variable: No such file or directory
grep: set: No such file or directory
grep: on: No such file or directory
grep: the: No such file or directory
grep: command: No such file or directory
grep: line: No such file or directory

This happens because the variable is expanded by the shell. When expanded, grep recognizes it as multiple arguments where a filename should be.

So the question is, how can we get around this?

Well, we know that grep accepts standard input.

grep searches the named input FILEs (or standard input if no files are named) for lines containing a match to the given PATTERN.

- Grep Man Page

Here is a demonstration of grep accepting standard input and matching the letter "a".

Demo of the Linux grep command accepting standard input

It seems we need to pass the variable as standard input to grep. There are multiple ways we can accomplish this. Let's demonstrate a couple easy methods.

Using echo to Create Standard Input

We can turn the variable into standard output (STDOUT) using the echo command.

Using echo to print variable contents to standard output

If we send the output of echo into a pipeline, grep will receive it on the other side as standard input (STDIN).

Using echo to grep patterns from a variable

Grep works well with standard input. This allows us to use grep to match a pattern from a variable. It's is not the most graceful solution, but it works.

To learn more about standard streams (STDIN, STDOUT, & STDERR) and Pipelines, read "Linux I/O, Standard Streams and Redirection".

Using a Here String

A here string is a stripped down version of a here document. It basically allows you to feed a variable to a command as standard input (STDIN). To use a here string, simple add three less than symbols in front of the variable name like so:

grep pattern from a variable using here string

Using the here string allows you to easily grep a string from a variable.

Counting the Instances of a String in a Variable

You can use the wc utility to count the number of times a string is found in a variable. To do this, you must first tell grep to only show the matching characters with the -o (--only-matching) option. Then you can pipe it to wc.

count instances of a string in a variable

Conclusion

When using Linux there are always several ways to accomplish a task. In this Linux quick tip we demonstrated how to grep a string from a variable. By converting a variable into the standard input stream we were able to pass it to grep. It's an interesting problem with an interesting solution.

If you have any questions, comments or additional methods for using grep to find patterns in variables, sound off in the comments below!