How to assign a grep command value to a variable in Linux/Unix

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)"

Examples

To display date and time using echo command:

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:

todays=$(date)

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"

Sample outputs:

Fig.01: grep store output to shell variable and echo back on screen

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"

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


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 1 comment so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
1 comment… add one
  • Sarah Williams Jan 6, 2017 @ 17:36

    That’s a good bash tip, but that grep may give you some unexpected output. It will also match viveki, vivek25, etc. Or, if you grep for username tom, you’ll also get tomcat. So you’ll either want to add a space to the end of your regex like ‘^tom ‘, or instead of a regex use -w for word matching. I would also add -m1 to match only once, so if you do accidentally match multiple lines you only get the first one.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum