{"id":191,"date":"2018-10-17T00:00:00","date_gmt":"2018-10-16T16:00:00","guid":{"rendered":"https:\/\/yeslq.com\/201810191.html"},"modified":"2019-05-07T10:43:29","modified_gmt":"2019-05-07T02:43:29","slug":"the-linux-command-line-command","status":"publish","type":"post","link":"https:\/\/yeslq.com\/?p=191","title":{"rendered":"The Linux Command Line&#8212;Command Substitution And Constants"},"content":{"rendered":"<h1 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\n<i style=\"font-size: x-small;\"><a href=\"http:\/\/linuxcommand.org\/tlcl.php\" style=\"color: #002740;\">The Linux Command Line&nbsp;<\/a><\/i><span style=\"font-size: xx-small;\">by William Shotts<\/span><\/h1>\n<div>\n<span style=\"font-size: xx-small;\"><br \/><\/span><\/div>\n<div>\n<span style=\"font-size: xx-small;\"><\/span><\/p>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\n<span style=\"font-size: xx-small;\">In the previous lesson, we learned how to create variables and perform expansions with them. In this lesson, we will extend this idea to show how we can substitute the results from a command.<\/span><\/div>\n<p><span style=\"font-size: xx-small;\"><br \/>\n<\/span><\/p>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\n<span style=\"font-size: xx-small;\">When we last left our script, it could create an HTML page that contained a few simple lines of text, including the host name of the machine which we obtained from the environment variable HOSTNAME. Now, we will add a time stamp to the page to indicate when it was last updated, along with the user that did it.<\/span><\/div>\n<p><span style=\"font-size: xx-small;\"><\/p>\n<div class=\"codeexample\" style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\">#!\/bin\/bash\n\n# sysinfo_page - A script to produce an HTML file\n\ntitle=\"System Information for\"\n\ncat &lt;&lt;- _EOF_\n    &lt;html&gt;\n    &lt;head&gt;\n        &lt;title&gt;\n        $title $HOSTNAME\n        &lt;\/title&gt;\n    &lt;\/head&gt;\n\n    &lt;body&gt;\n    &lt;h1&gt;$title $HOSTNAME&lt;\/h1&gt;\n    &lt;p&gt;Updated on $(date +\"%x %r %Z\") by $USER&lt;\/p&gt;\n    &lt;\/body&gt;\n    &lt;\/html&gt;\n_EOF_\n       \n<\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nAs you can see, we employed another environment variable,&nbsp;USER, to get the user name. In addition, we used this strange looking thing:<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\n$(date +&#8221;%x %r %Z&#8221;)<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nThe characters &#8220;$( )&#8221; tell the shell, &#8220;substitute the results of the enclosed command.&#8221; In our script, we want the shell to insert the results of the command&nbsp;date +&#8221;%x %r %Z&#8221;&nbsp;which expresses the current date and time. The&nbsp;<a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/date1.html\" style=\"color: #002740;\">date<\/a>&nbsp;command has many features and formatting options. To look at them all, try this:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;date &#8211;help | less<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nBe aware that there is an older, alternate syntax for &#8220;$(command)&#8221; that uses the backtick character &#8221; ` &#8220;. This older form is compatible with the original Bourne shell (sh). I tend not to use the older form since I am teaching modern&nbsp;bash&nbsp;here, not&nbsp;sh, and besides, I think backticks are ugly. The bash shell fully supports scripts written for sh, so the following forms are equivalent:<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\n$(command)<br \/>`command`<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\nAssigning A Command&#8217;s Result To A Variable<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nYou can also assign the results of a command to a variable:<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nright_now=$(date +&#8221;%x %r %Z&#8221;)<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nYou can even nest the variables (place one inside another), like this:<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nright_now=$(date +&#8221;%x %r %Z&#8221;)<br \/>time_stamp=&#8221;Updated on $right_now by $USER&#8221;<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\nConstants<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nAs the name variable suggests, the content of a variable is subject to change. This means that it is expected that during the execution of your script, a variable may have its content modified by something you do.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nOn the other hand, there may be values that, once set, should never be changed. These are called<i>constants<\/i>. I bring this up because it is a common idea in programming. Most programming languages have special facilities to support values that are not allowed to change. Bash also has these facilities but, to be honest, I never see it used. Instead, if a value is intended to be a constant, it is given an uppercase name to remind the programmer that it should be considered a constant even if it&#8217;s not being enforced.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nEnvironment variables are usually considered constants since they are rarely changed. Like constants, environment variables are given uppercase names by convention. In the scripts that follow, I will use this convention &#8211; uppercase names for constants and lowercase names for variables.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nSo with everything we know, our program looks like this:<\/div>\n<div class=\"codeexample\" style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\">#!\/bin\/bash\n\n# sysinfo_page - A script to produce an HTML file\n\ntitle=\"System Information for $HOSTNAME\"\nRIGHT_NOW=$(date +\"%x %r %Z\")\nTIME_STAMP=\"Updated on $RIGHT_NOW by $USER\"\n\ncat &lt;&lt;- _EOF_\n    &lt;html&gt;\n    &lt;head&gt;\n        &lt;title&gt;\n        $title\n        &lt;\/title&gt;\n    &lt;\/head&gt;\n\n    &lt;body&gt;\n    &lt;h1&gt;$title&lt;\/h1&gt;\n    &lt;p&gt;$TIME_STAMP&lt;\/p&gt;\n    &lt;\/body&gt;\n    &lt;\/html&gt;\n_EOF_\n       \n<\/pre>\n<\/div>\n<div class=\"pagenav\" style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; padding: 1em;\">\n<\/div>\n<p><\/span><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The Linux Command Line&nbsp;by William Shotts In the pr &#8230; <a title=\"The Linux Command Line&#8212;Command Substitution And Constants\" class=\"read-more\" href=\"https:\/\/yeslq.com\/?p=191\" aria-label=\"\u9605\u8bfb The Linux Command Line&#8212;Command Substitution And Constants\">\u9605\u8bfb\u66f4\u591a<\/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":[51,52,53],"class_list":["post-191","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-linux","tag-shell","tag-the-linux-command-line"],"_links":{"self":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=191"}],"version-history":[{"count":1,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/191\/revisions"}],"predecessor-version":[{"id":313,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/191\/revisions\/313"}],"wp:attachment":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}