Skip to content

Grep In Shell Variable Assignment

How do I store grep command output in shell variable? What is the syntax to store the command output into a variable in Linux or Unix?

You can use the grep command for any given input files, selecting lines that match one or more patterns. By default the output shown on screen. But, you can store output to variable in your shell scripts.

Syntax: Command substitution

Command substitution means nothing more but to run a shell command and store its output to a variable or display back using echo command. The syntax is:

VAR=`command-name`VAR="`grep word /path/to/file`"## or ##   VAR=$(command-name)VAR="$(grep word /path/to/file)"

VAR=`command-name` VAR="`grep word /path/to/file`" ## or ## VAR=$(command-name) VAR="$(grep word /path/to/file)"


To display date and time using echo command:

echo"Today is $(date)"## or ##echo"Today is `date`"

echo "Today is $(date)" ## or ## echo "Today is `date`"

You can store command output to a shell variable using the following syntax:

To store current date and time to a variable called todays:


You can display value of $todays, enter:

echo "$todays"

In this example use grep command to search for a username called vivek and store output to a variable called myuser:

myuser="$(grep '^vivek' /etc/passwd)"echo"$myuser"

myuser="$(grep '^vivek' /etc/passwd)" echo "$myuser"

Sample outputs:

You can store the output of a grep command in a variable at the same time as printing the output using the following tee command based syntax:
foo="$(grep '^vivek' /etc/passwd | tee /dev/tty) "echo"$foo"

foo="$(grep '^vivek' /etc/passwd | tee /dev/tty) " echo "$foo"

This is useful to direct output from a grep command to the shell variable and display on screen at the same time.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

A shell assignment is a single word, with no space after the equal sign. So what you wrote assigns an empty value to ; furthermore, since the assignment is grouped with a command, it makes an environment variable and the assignment is local to that particular command, i.e. only the call to sees the assigned value.

You want to capture the output of a command, so you need to use command substitution:

(Some literature shows an alternate syntax ; the backquote syntax is equivalent to the dollar-parentheses syntax except that quoting inside backquotes is weird sometimes, so just use .)

Other remarks about your script:

  • Combining (sort by time) with (don't sort) doesn't make sense; just use .
  • Rather than using to match screenshots, it's clearer to pass a wildcard to and use to capture the first file:

  • It's generally a bad idea to parse the output of . This could fail quite badly if you have file names with nonprintable characters. However, sorting files by date is difficult without , so it's an acceptable solution if you know you won't have unprintable characters or backslashes in file names.

  • Always use double quotes around variable substitutions, i.e. here write

    Without double quotes, the value of the variable is reexpanded, which will cause trouble if it contains whitespace or other special characters.

  • You don't need semicolons at the end of a line. They're redundant but harmless.
  • In a shell script, it's often a good idea to include . This tells the shell to exit if any command fails (by returning a nonzero status).

If you have GNU find (in particular if you're running non-embedded Linux or Cygwin), there's another approach to finding the most recent file: have list the files and their dates, and use and to extract the youngest file.

If you're willing to write this script in zsh instead of bash, there's a much easier way to catch the newest file, because zsh has glob qualifiers that permit wildcard matches not only on names but also on file metadata. The part after the pattern is the glob qualifiers; sorts matches by increasing age (i.e. by modification time, newest first) and extracts the first match only. The whole match needs to be in parentheses because it's technically an array, since globbing returns a list of files, even if the means that in this particular case the list contains (at most) one file.

answered Jul 3 '11 at 21:52