<h2 id="the-environment-more-collateral-bash-tips">The Environment <small><small>(More collateral Bash tips)</small></small></h2>
<h3 id="overview">Overview</h3>
<p>You will probably have seen references to <em>The Environment</em> in various contexts relating to shells, shell scripts, scripts in other languages and compiled programs.</p>
<p>In Unix and Unix-like operating systems an environment is maintained by the shell, and we will be looking at how Bash deals with this in this episode. When a script, program or subprocess is invoked it is given an array of strings called the <em>environment</em>. This is a list of name-value pairs, of the form <code>name=value</code>.</p>
<h4 id="using-the-environment">Using the environment</h4>
<p>The environment is used to convey various pieces of information to the executing script or program. For example, two standard variables provided by the shell are <code>'HOME'</code>, which is set to the current user’s home directory and <code>'PWD</code>, set to the current working directory. The shell user can set, change, remove and view environment variables for their own purposes as we will see in this episode. The Bash shell itself creates and in some cases manages environment variables.</p>
<p>The environment contains global data which is passed down to subprocesses (<em>child</em> processes) by copying. However, it is not possible for a subprocess to pass information <b>back</b> to the superior (<em>parent</em>) process.</p>
<h4 id="viewing-the-environment">Viewing the environment</h4>
<p>You can view the environment in a number of ways.</p>
<ul>
<li><p>From the command line the command <code>printenv</code> can do this (this is usually but not always a stand-alone command: it’s <code>/usr/bin/printenv</code> on my Debian system). We will look at this command later.</p></li>
<li><p>The command <code>env</code> without any arguments does the same thing as <code>printenv</code> without arguments. This is actually a tool to run a program in a modified environment which we will look at later. The environment printing capability can be regarded as more of a bonus feature.</p></li>
<li><p>Scripting languages like <code>awk</code> (as well as Python and Perl, to name just a few) can view and manipulate the environment.</p></li>
<li><p>Compiled languages such as <code>C</code> can do this too of course.</p></li>
<li><p>There are other commands that will show the environment, and we will look at some of these briefly.</p></li>
</ul>
<h4 id="changing-variables-in-the-environment">Changing variables in the environment</h4>
<p>The variables in the environment are not significantly different from the <em>shell parameters</em> we have seen throughout this Bash Tips series. The only difference is that they are marked for <em>export</em> to commands and sub-shells. You will often see variables (or parameters) in the environment referred to as <em>environment variables</em>. The Bash manual makes a distinction between ordinary parameters (variables) and environment variables, but many other sources are less precise about this in my experience.</p>
<p>The standard variables in the environment have upper-case names (<code>HOME</code>, <code>SHELL</code>, <code>PWD</code>, etc), but there is no reason why a variable you create should not be in lower or mixed case. In fact, the Bash manual suggests that you should avoid using all upper-case names so as not to clash with Bash’s variables.</p>
<p>Variables can be created and changed a number of ways.</p>
<ul>
<li>They can be set up at login time (globally or locally) through various standard configuration files. It is intended to look at this subject in an upcoming episode so we will leave discussing the subject until then.</li>
<li>By preceding the command or script invocation with <em>name=value</em> expressions which will temporarily place these variables into the environment for the command</li>
<li>Using the <code>export</code> command</li>
<li>Using the <code>declare</code> command with the <code>-x</code> option</li>
<li>The value of an environment variable (once established) can be changed at any time in the sub-shell with a command like <code>myvar=42</code>, just as for a normal variable</li>
<li>The <code>export</code> command can also be used to turn off the <em>export</em> marker on a variable</li>
<li>Deletion is performed with the <code>unset</code> command (as seen earlier in the series)</li>
</ul>
<p>We will look at all of these features in more detail later in the episode.</p>
<h2 id="long-notes">Long notes</h2>
<p>I have provided detailed notes as usual for this episode, and these can be <a href="https://hackerpublicradio.org/eps/hpr3013/full_shownotes.html">viewed here</a>.</p>
<h2 id="links">Links</h2>
<ul>
<li><a href="https://www.gnu.org/software/bash/manual/bash.html">“<em>GNU BASH Reference Manual</em>”</a>
<ul>
<li>Section <a href="https://www.gnu.org/software/bash/manual/bash.html#Command-Execution-Environment">“3.7.3 Command Execution Environment”</a></li>
<li>Section <a href="https://www.gnu.org/software/bash/manual/bash.html#Environment">“3.7.4 Environment”</a></li>
</ul></li>
<li><a href="https://www.gnu.org/software/coreutils/manual/"><em>GNU Coreutils Manual</em></a>
<ul>
<li>Section <a href="https://www.gnu.org/software/coreutils/manual/html_node/env-invocation.html">23.2 env: Run a command in a modified environment</a></li>
</ul></li>
<li><p><a href="https://en.wikipedia.org/wiki/Env">Wikipedia page on the <code>env</code> command</a></p></li>
<li><p><a href="https://hackerpublicradio.org/series/0042.html">HPR series: <em>Bash Scripting</em></a></p></li>
<li>Previous episodes under the heading <em>Bash Tips</em>:<br />
<small><strong>Episode number</strong></small>
<ol>
<li><a href="https://hackerpublicradio.org/eps.php?id=1648">HPR episode 1648 <code>"</code><em>Bash parameter manipulation</em><code>"</code></a></li>
<li><a href="https://hackerpublicradio.org/eps.php?id=1843">HPR episode 1843 <code>"</code><em>Some Bash tips</em><code>"</code></a></li>
<li><a href="https://hackerpublicradio.org/eps.php?id=1884">HPR episode 1884 <code>"</code><em>Some more Bash tips</em><code>"</code></a></li>
<li><a href="https://hackerpublicradio.org/eps.php?id=1903">HPR episode 1903 <code>"</code><em>Some further Bash tips</em><code>"</code></a></li>
<li><a href="https://hackerpublicradio.org/eps.php?id=1951">HPR episode 1951 <code>"</code><em>Some additional Bash tips</em><code>"</code></a></li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2045">HPR episode 2045 <code>"</code><em>Some other Bash tips</em><code>"</code></a></li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2278">HPR episode 2278 <code>"</code><em>Some supplementary Bash tips</em><code>"</code></a></li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2293">HPR episode 2293 <code>"</code><em>More supplementary Bash tips</em><code>"</code></a></li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2639">HPR episode 2639 <code>"</code><em>Some ancillary Bash tips - 9</em><code>"</code></a></li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2649">HPR episode 2649 <code>"</code><em>More ancillary Bash tips - 10</em><code>"</code></a></li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2659">HPR episode 2659 <code>"</code><em>Further ancillary Bash tips - 11</em><code>"</code></a></li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2669">HPR episode 2669 <code>"</code><em>Additional ancillary Bash tips - 12</em><code>"</code></a></li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2679">HPR episode 2679 <code>"</code><em>Extra ancillary Bash tips - 13</em><code>"</code></a></li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2689">HPR episode 2689 <code>"</code><em>Bash Tips - 14 (Some auxiliary Bash tips)</em><code>"</code></a></li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2699">HPR episode 2699 <code>"</code><em>Bash Tips - 15 (More auxiliary Bash tips)</em><code>"</code></a></li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2709">HPR episode 2709 <code>"</code><em>Bash Tips - 16 (Further auxiliary Bash tips)</em><code>"</code></a></li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2719">HPR episode 2719 <code>"</code><em>Bash Tips - 17 (Additional auxiliary Bash tips)</em><code>"</code></a></li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2729">HPR episode 2729 <code>"</code><em>Bash Tips - 18 (Extra auxiliary Bash tips)</em><code>"</code></a></li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2739">HPR episode 2739 <code>"</code><em>Bash Tips - 19 (Supplemental auxiliary Bash tips)</em><code>"</code></a></li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2756">HPR episode 2756 <code>"</code><em>Bash Tips - 20 (Some collateral Bash tips)</em><code>"</code></a></li>
</ol></li>
</ul>
<ul>
<li>Resources:
<ul>
<li>Examples:
<ul>
<li><a href="https://hackerpublicradio.org/eps/hpr3013/bash21_ex1.sh">bash21_ex1.sh</a></li>
<li><a href="https://hackerpublicradio.org/eps/hpr3013/bash21_ex2.sh">bash21_ex2.sh</a></li>
<li><a href="https://hackerpublicradio.org/eps/hpr3013/bash21_ex3.awk">bash21_ex3.awk</a></li>
<li><a href="https://hackerpublicradio.org/eps/hpr3013/bash21_ex4.sh">bash21_ex4.sh</a></li>
<li><a href="https://hackerpublicradio.org/eps/hpr3013/bash21_ex5.sh">bash21_ex5.sh</a></li>
<li><a href="https://hackerpublicradio.org/eps/hpr3013/bash21_ex6.sh">bash21_ex6.sh</a></li>
<li><a href="https://hackerpublicradio.org/eps/hpr3013/bash21_ex6_1.cfg">bash21_ex6_1.cfg</a></li>
<li><a href="https://hackerpublicradio.org/eps/hpr3013/bash21_ex6_2.cfg">bash21_ex6_2.cfg</a></li>
</ul></li>
</ul></li>
</ul>
