{"id":184,"date":"2020-08-18T19:23:47","date_gmt":"2020-08-18T20:23:47","guid":{"rendered":"http:\/\/www.linux-tutorial.info\/?page_id=77"},"modified":"2020-08-22T19:26:19","modified_gmt":"2020-08-22T20:26:19","slug":"this-is-the-page-title-toplevel-18","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=184","title":{"rendered":"Shell Variables"},"content":{"rendered":"\n<title>Shell Variables<\/title>\n<question id=\"36\" text=\"What is the term used to describe the variables and other information a shell uses?\" \/>\n<p>\nThe shell&#8217;s <glossary>environment<\/glossary>\nis all the information that the <glossary>shell<\/glossary>\nwill use as it runs. This includes such things as your command\n<glossary>search path<\/glossary>, your\n<em>logname<\/em> (the name you logged in under), and the <glossary>terminal<\/glossary>\ntype you are using. Collectively, they are referred to as your\n<i><glossary>environment<\/glossary> variables<\/i>\nand individually, as the &#8220;so-and-so&#8221;\nenvironment <glossary>variable<\/glossary>,\nsuch as the TERM <glossary>environment<\/glossary>\nvariable, which contains the type of <glossary>terminal<\/glossary>\nyou  are using.\n<\/p>\n<\/p>\n<concept id=\"\" description=\"Environment variables are typically those variables accessable from your shell.\" \/>\n<question id=\"\" type=\"\" text=\"Environment variables are typically those variables accessable from your shell\" \/>\n<p>\nNote that not every variable is a &#8220;environment variable&#8221;. Any shell script can set variables as\nit pleases, but they have no effect on the <glossary>environment<\/glossary>. Typically variables\nare referred to as environment variables if the variable is accessible directly from the\n<glossary>shell<\/glossary>.\nYou might hear the term more strictly used to define variable that are defined by one of the\nrespective shell&#8217;s configuration files.\n<\/p>\n<p>\nWhen you log in, most of these are set for you in one way or another.  (The\nmechanism that sets all <glossary>environment<\/glossary>\nvariables is shell-dependent, so we will talk about it when we get to the individual\nshells.) Each <glossary>environment<\/glossary>\n<glossary>variable<\/glossary> can be viewed by simply typing echo $VARIABLE. For example, if I\ntype\n<\/p>\n<p>\n<commandexample command=\"echo\">echo $LOGNAME<\/commandexample>\n<\/p>\n<p>\nI get:\n<\/p>\n<p>\n<screenoutput>jimmo<\/screenoutput>\n<\/p>\n<p>\nTyping\n<p>\n<commandexample command=\"echo\">echo $TERM<\/commandexample>\n<p>\nI get:\n<\/p>\n<p>\n<screenoutput>ansi<\/screenoutput>\n<\/p>\n<p>\nIn general, variables that are pre-defined by the system (e.g. PATH, LOGNAME, HOME) are\nwritten in capital letters. Note that this is not a requirement as there are exceptions.\n<\/p>\n<question id=\"37\" text=\"When a shell variable is defined, it is automatically available to subsequent processes.\" \/>\n<concept id=\"92\" description=\"When a shell variable is defined, it is NOT automatically available to subsequent processes.\" \/>\n<question id=\"38\" text=\"The default system-wide environment is defined in which file?\" \/>\n<concept id=\"36\" description=\"The system-wide default evironment is defined in \/etc\/profile.\" \/>\n<question id=\"\" type=\"mc\" text=\"What must you do in order to make variables available to child-processes?\" \/>\n<question id=\"\" type=\"mc\" text=\"What are you doing when you 'export' a variable?\" \/>\n<p>\nNote that <glossary>shell<\/glossary> variables are only accessible from the current shell.\nIn order for them to\nbe accessible to child processes (i.e. sub-processes) they must be made available\nusing the <command>export<\/command> command. In the system-wide shell configuration\nfile or &#8220;profile&#8221; (\/etc\/profile) many variables, such as PATH are exported.\nMore information on processes can be found in the\n<tutorial id=3>section on processes<\/tutorial>.\n<p>\nIt is very common that users&#8217; <glossary>shell<\/glossary> prompt is defined by the system. For example, you might have\nsomething that looks like this:\n<\/p>\n<commandexample command=\"bash\">PS1=&#8217;\\u@\\h:\\w> &#8216;<\/commandexample>\n<p>\nWhat this does is to set the first level prompt variable PS1 to include the username, hostname\nand the current working directory. This ends up looking something like this:\n<\/p>\n<p><screenoutput>jimmo@linux:\/tmp><\/screenoutput>\n<\/p>\n<p>Adding the <keyinput>\\A<keyinput> to display the time, we end up with something that\nlooks like this:<\/p>\n<screenoutput>\n10:09 jimmo@linux:\/tmp>\n<\/screenoutput>\n<table>\n  <tbody>\n    <tr>\n      <td>Variable<\/td>\n      <td>Meaning<\/td>\n    <\/tr>\n    <tr>\n      <td>\\u <\/td>\n      <td>Username <\/td>\n    <\/tr>\n    <tr>\n      <td>\\h  <\/td>\n      <td>Hostname <\/td>\n    <\/tr>\n\t<tr>\n      <td>\\H <\/td>\n      <td>The fully-qualified hostname  <\/td>\n    <\/tr>\n    <tr>\n      <td>\\w <\/td>\n      <td>Current working directory  <\/td>\n    <\/tr>\n    <tr>\n      <td>\\d <\/td>\n      <td>date <\/td>\n    <\/tr>\n    <tr>\n      <td>\\t <\/td>\n      <td>the current time in 24-hour HH:MM:SS format<\/td>\n    <\/tr>\n    <tr>\n      <td>\\T<\/td>\n      <td>the current time in 12-hour HH:MM:SS format <\/td>\n    <\/tr>\n    <tr>\n      <td>\\@ <\/td>\n      <td>the current time in 12-hour am\/pm format  <\/td>\n    <\/tr>\n    <tr>\n      <td>\\A <\/td>\n      <td>the current time in 24-hour HH:MM format  <\/td>\n    <\/tr>\n    <tr>\n      <td>\\l <\/td>\n      <td>the basename of the shell&#8217;s terminal device<\/td>\n    <\/tr>\n    <tr>\n      <td>\\e<\/td>\n      <td>Escape character<\/td>\n    <\/tr>\n    <tr>\n      <td>\n <\/td>\n      <td>newline<\/td>\n    <\/tr>\n    <tr>\n      <td> <\/td>\n      <td>carriage return <\/td>\n    <\/tr>\n  <\/tbody>\n<\/table>\n<p>\nOne way of using the escape character in your prompt is to send a terminal control\nsequence. The can be used, for example, to change the prompt so that the time is\nshown in red:\n<\/p>\n<commandexample command=\"bash\">PS1=&#8217;\\e[31m\\A\\e[0m \\u@\\h:\\w> &#8216;\n<\/commandexample>\n<p>Which then looks like this:\n<\/p>\n<screenoutput><font color=\"red\">10:09<\/font> jimmo@linux:\/tmp><\/screenoutput>\n<question id=\"\" type=\"mc\" text=\"What special variable contains the PID of the current shell?\" \/>\n<question id=\"\" type=\"mc\" text=\"What special variable contains the status of the last command executed in the foreground?\" \/>\n<p>\nThere a several variables that are special for the shell. For example, $$ refers to the <glossary>PID<\/glossary>\nof the shell currently running. This is commonly used to the PID into a file, which can be checked later, for example to verify the process is still running. So, you might have something like this:\n<\/p>\n<commandexample command=\"bash\">PID=$$<\/commandexample>\n<p>\nThe next special variable is <keyinput>$?<\/keyinput> and contains the status of the last command executed in the foreground. This is often used for things like if-statements when you wnat to check whether a command successfully completed before continuing. For example:\n<\/p>\n<commandexample command=\"bash\">\ngrep SOMEDATA filename\nSTATUS=$?\n<\/commandexample>\n<p>\nThis would store the exit code of the <command>grep<\/command> command.\n<\/p>\n<question id=\"\" type=\"mc\" text=\"Assume you just started a program in the background but need to stop it immediate. Which command would you use?\" \/>\n<p>\nThe variable <keyinput>$!<\/keyinput> contains the process ID of the last process executed in the background. This is a good way of keeping track of multiple processes, which are all run in the background.\n<\/p>\n<p>\nYou will find that on most system quite a few variables are set by default. You can display all of them at once simply by issuing the shell-builtin <command name=\"bash\">set<\/command>. Note that this will also show you\nfunctions, as well as variables. You can also issue the <command>env<\/command>  command, which just shows you the environment variables.\n<\/p>\n<question id=\"\" type=\"TF\" text=\"It is possible to set a variable and not have it appear in the output of the 'env'.\" \/>\n<concept id=\"\" description=\"It is possible to set a variable and not have it appear in the output of the 'env'.\" \/>\n<question id=\"\" type=\"TF\" text=\"The 'set' command shows you all variables that have been set, whereas the 'env' commands shows you only those that have been exported.\" \/>\n<concept id=\"\" description=\"The 'set' command shows you all variables that have been set, whereas the 'env' commands shows you only those that have been exported.\" \/>\n<question id=\"\" type=\"mc\" text=\"What command would show you all variables that have been exported?\" \/>\n<p>\nNote this these two commands do not show you the same variables. I think it easiest to think that <command name=\"bash\">set<\/command> will show you all of the variables that have been set, whereas <command>env<\/command> will only show you variables that are part of your environment. That it, it will only show you variables that have been exported. That is, it is possible to set a variable and not have it appear in the output of the <command>env<\/command> command.\n<\/p>\n<p>\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Shell Variables The shell&#8217;s environment is all the information that the shell will use as it runs. This includes such things as your command search path, your logname (the name you logged in under), and the terminal type you are &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=184\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-184","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/184","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=184"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/184\/revisions"}],"predecessor-version":[{"id":701,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/184\/revisions\/701"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}