{"id":192,"date":"2018-10-16T12:00:00","date_gmt":"2018-10-16T04:00:00","guid":{"rendered":"https:\/\/yeslq.com\/201810192.html"},"modified":"2019-05-07T10:43:26","modified_gmt":"2019-05-07T02:43:26","slug":"the-linux-command-line-variables-2","status":"publish","type":"post","link":"http:\/\/yeslq.com\/?p=192","title":{"rendered":"The Linux Command Line&#8212;Variables"},"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 class=\"codeexample\" style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"><span style=\"font-size: xx-small;\">#!\/bin\/bash\n\n# sysinfo_page - A script to produce an HTML file\n\ncat &lt;&lt;- _EOF_\n    &lt;html&gt;\n    &lt;head&gt;\n        &lt;title&gt;\n        My System Information\n        &lt;\/title&gt;\n    &lt;\/head&gt;\n\n    &lt;body&gt;\n    &lt;h1&gt;My System Information&lt;\/h1&gt;\n    &lt;\/body&gt;\n    &lt;\/html&gt;\n_EOF_\n       \n<\/span><\/pre>\n<\/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;\">Now that we have our script working, let&#8217;s improve it. First off, we&#8217;ll make some changes because we want to be lazy. In the script above, we see that the phrase &#8220;My System Information&#8221; is repeated. This is wasted typing (and extra work!) so we improve it like this:<\/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=\"My System Information\"\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;\/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 added a line to the beginning of the script and replaced the two occurrences of the phrase &#8220;My System Information&#8221; with&nbsp;$title.<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\nVariables<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nWhat we have done is to introduce a very fundamental idea that appears in almost every programming language,&nbsp;<i>variables<\/i>. Variables are areas of memory that can be used to store information and are referred to by a name. In the case of our script, we created a variable called &#8220;title&#8221; and placed the phrase &#8220;My System Information&#8221; into memory. Inside the here script that contains our HTML, we use &#8220;$title&#8221; to tell the shell to perform&nbsp;<i>parameter expansion<\/i>&nbsp;and replace the name of the variable with the variable&#8217;s contents.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nWhenever the shell sees a word that begins with a &#8220;$&#8221;, it tries to find out what was assigned to the variable and substitutes it.<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\nHow To Create A Variable<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nTo create a variable, put a line in your script that contains the name of the variable followed immediately by an equal sign (&#8220;=&#8221;). No spaces are allowed. After the equal sign, assign the information you wish to store.<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\nWhere Do Variable Names Come From?<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nYou make them up. That&#8217;s right; you get to choose the names for your variables. There are a few rules.<\/div>\n<ol style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\n<li>Names must start with a letter.<\/li>\n<li>A name must not contain embedded spaces. Use underscores instead.<\/li>\n<li>You cannot use punctuation marks.<\/li>\n<\/ol>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\nHow Does This Increase Our Laziness?<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nThe addition of the&nbsp;title&nbsp;variable made our life easier in two ways. First, it reduced the amount of typing we had to do. Second and more importantly, it made our script easier to maintain.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nAs you write more and more scripts (or do any other kind of programming), you will learn that programs are rarely ever finished. They are modified and improved by their creators and others. After all, that&#8217;s what open source development is all about. Let&#8217;s say that you wanted to change the phrase &#8220;My System Information&#8221; to &#8220;Linuxbox System Information.&#8221; In the previous version of the script, you would have had to change this in two locations. In the new version with the&nbsp;titlevariable, you only have to change it in one place. Since our script is so small, this might seem like a trivial matter, but as scripts get larger and more complicated, it becomes very important.<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\nEnvironment Variables<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nWhen you start your shell session, some variables are already set by the startup file we looked at earlier. To see all the variables that are in your environment, use the&nbsp;printenv&nbsp;command. One variable in your environment contains the host name for your system. We will add this variable to our script like so:<\/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\"\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;\/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;\">\nNow our script will always include the name of the machine on which we are running. Note that, by convention, environment variables names are uppercase.<\/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 #!\/bin\/ba &#8230; <a title=\"The Linux Command Line&#8212;Variables\" class=\"read-more\" href=\"http:\/\/yeslq.com\/?p=192\" aria-label=\"\u9605\u8bfb The Linux Command Line&#8212;Variables\">\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-192","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-linux","tag-shell","tag-the-linux-command-line"],"_links":{"self":[{"href":"http:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/192","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=192"}],"version-history":[{"count":1,"href":"http:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/192\/revisions"}],"predecessor-version":[{"id":312,"href":"http:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/192\/revisions\/312"}],"wp:attachment":[{"href":"http:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=192"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}