{"id":12143,"date":"2019-03-22T12:00:18","date_gmt":"2019-03-22T12:00:18","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=12143"},"modified":"2019-03-22T12:00:18","modified_gmt":"2019-03-22T12:00:18","slug":"how-to-assign-output-of-a-linux-command-to-a-variable","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/22\/how-to-assign-output-of-a-linux-command-to-a-variable\/","title":{"rendered":"How To Assign Output of a Linux Command to a Variable"},"content":{"rendered":"<p>When you run a command, it produces some kind of output: either the result of a program is suppose to produce or status\/error messages of the program execution details. Sometimes, you may want to store the output of a command in a variable to be used in a later operation.<\/p>\n<p>In this post, we will review the different ways of assigning the output of a shell command to a variable, specifically useful for shell scripting purpose.<\/p>\n<p>To store the output of a command in a variable, you can use the shell command substitution feature in the forms below:<\/p>\n<pre>variable_name=$(command)\r\nvariable_name=$(command [option ...] arg1 arg2 ...)\r\nOR\r\nvariable_name='command'\r\nvariable_name='command [option ...] arg1 arg2 ...'\r\n<\/pre>\n<p>Below are a few examples of using command substitution.<\/p>\n<p>In this first example, we will store the value of\u00a0<code>who<\/code>\u00a0(which shows who is logged on the system) command in the variable\u00a0<code>CURRENT_USERS<\/code>\u00a0user:<\/p>\n<pre>$ CURRENT_USERS=$(who)\r\n<\/pre>\n<p>Then we can use the variable in a sentence displayed using the\u00a0<a href=\"https:\/\/www.tecmint.com\/echo-command-in-linux\/\" target=\"_blank\" rel=\"noopener\">echo command<\/a>\u00a0like so:<\/p>\n<pre>$ echo -e \"The following users are logged on the system:\\n\\n $CURRENT_USERS\"\r\n<\/pre>\n<p>In the command above: the flag\u00a0<code>-e<\/code>\u00a0means interpret any escape sequences ( such as\u00a0<code>\\n<\/code>\u00a0for newline) used. To avoid wasting time as well as memory, simply perform the command substitution within the\u00a0<a href=\"https:\/\/www.tecmint.com\/echo-command-in-linux\/\" target=\"_blank\" rel=\"noopener\">echo command<\/a>\u00a0as follows:<\/p>\n<pre>$ echo -e \"The following users are logged on the system:\\n\\n $(who)\"\r\n<\/pre>\n<div id=\"attachment_24223\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/Shows-Current-Logged-Users-in-Linux.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24223\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/Shows-Current-Logged-Users-in-Linux.png\" alt=\"Shows Current Logged Users in Linux\" width=\"762\" height=\"192\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Shows Current Logged Users in Linux<\/p>\n<\/div>\n<p>Next, to demonstrate the concept using the second form; we can store the total number of files in the current working directory in a variable called\u00a0<code>FILES<\/code>\u00a0and\u00a0<strong>echo<\/strong>\u00a0it later as follows:<\/p>\n<pre>$ FILES=`sudo find . -type f -print | wc -l`\r\n$ echo \"There are $FILES in the current working directory.\"\r\n<\/pre>\n<div id=\"attachment_24224\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/Show-Number-of-Files-in-Directory.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24224\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/Show-Number-of-Files-in-Directory.png\" alt=\"Show Number of Files in Directory\" width=\"763\" height=\"140\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Show Number of Files in Directory<\/p>\n<\/div>\n<p>That\u2019s it for now, in this article, we explained the methods of assigning the output of a shell command to a variable. You can add your thoughts to this post via the feedback section below.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/assign-linux-command-output-to-variable\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you run a command, it produces some kind of output: either the result of a program is suppose to produce or status\/error messages of the program execution details. Sometimes, you may want to store the output of a command in a variable to be used in a later operation. In this post, we will &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/22\/how-to-assign-output-of-a-linux-command-to-a-variable\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How To Assign Output of a Linux Command to a Variable&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-12143","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=12143"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12143\/revisions"}],"predecessor-version":[{"id":12146,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12143\/revisions\/12146"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=12143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=12143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=12143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}