﻿<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://purl.org/atom/ns#">
	<link xmlns="http://purl.org/atom/ns#" type="text/html" rel="alternate" href="http://sial.org/blog/" title="Jeremy Mates’s Blog"/>
	<title xmlns="http://purl.org/atom/ns#">Jeremy Mates’s Blog</title>
	<entry xmlns="http://purl.org/atom/ns#" xmlns:default="http://www.w3.org/1999/xhtml">
		<title xmlns="http://purl.org/atom/ns#">A Brief Primer on Unix Environment Variables</title>
		<dc:subject>Computers</dc:subject>
		<summary xmlns="http://purl.org/atom/ns#">summary</summary>
		<content xmlns="http://purl.org/atom/ns#" mode="escaped">&lt;p&gt;The shell used will not make a significant difference, assuming one adheres to the Bourne—or ideally a Bourne derived—shell. At present, I favor &lt;a href="http://www.zsh.org/"&gt;ZSH&lt;/a&gt;.&lt;/p&gt;
&lt;p class="sial-block-shell"&gt;$ &lt;kbd&gt;echo $SHELL is being used&lt;/kbd&gt;
/bin/zsh is being used
$ &lt;kbd&gt;echo this shell uses $(tty), process id $$&lt;/kbd&gt;
this shell uses /dev/ttyp8, process id 19741
$ &lt;kbd&gt;echo $MY_ENV_VAR&lt;/kbd&gt;

$ &lt;kbd&gt;MY_ENV_VAR=&quot;something of value&quot;&lt;/kbd&gt;
$ &lt;kbd&gt;echo $MY_ENV_VAR&lt;/kbd&gt;
something of value
$ &lt;kbd&gt;$SHELL&lt;/kbd&gt;
$ &lt;kbd&gt;echo this shell uses $(tty), process id $$&lt;/kbd&gt;
this shell uses /dev/ttyp8, process id 19875
$ &lt;kbd&gt;echo $MY_ENV_VAR&lt;/kbd&gt;

$ &lt;kbd&gt;exit&lt;/kbd&gt;
$ &lt;kbd&gt;echo $MY_ENV_VAR&lt;/kbd&gt;
something of value&lt;/p&gt;
&lt;p&gt;Note that though &lt;tt&gt;MY_ENV_VAR&lt;/tt&gt; shows a value in the first shell, it does not in the subshell (opened via the &lt;tt&gt;$SHELL&lt;/tt&gt; command). After closing the subshell, the custom &lt;tt&gt;MY_ENV_VAR&lt;/tt&gt; is still defined in the original shell. Moving on, under the same session:&lt;/p&gt;
&lt;p class="sial-block-shell"&gt;$ &lt;kbd&gt;export MY_ENV_VAR&lt;/kbd&gt;
$ &lt;kbd&gt;echo $MY_ENV_VAR&lt;/kbd&gt;
something of value
$ &lt;kbd&gt;$SHELL&lt;/kbd&gt;
$ &lt;kbd&gt;echo this shell uses $(tty), process id $$&lt;/kbd&gt;
this shell uses /dev/ttyp8, process id 22585
$ &lt;kbd&gt;echo $MY_ENV_VAR&lt;/kbd&gt;
something of value
$ &lt;kbd&gt;exit&lt;/kbd&gt;
$ &lt;kbd&gt;echo $MY_ENV_VAR&lt;/kbd&gt;
something of value&lt;/p&gt;
&lt;p&gt;The &lt;tt&gt;export&lt;/tt&gt; builtin ensures child processes inherit the custom environment setting. The &lt;tt&gt;export&lt;/tt&gt; only needs to be done once on the variable. In modern Bourne shells, this can either be done via a single &lt;tt&gt;export SOME_ENV=some_value&lt;/tt&gt; command, or, as shown above, separate commands.&lt;/p&gt;
&lt;p&gt;Processes may strip environment variables, usually for security reasons. This would account for otherwise exported variables not being present in child processes. For confirmation, run &lt;a href="http://sial.org/howto/debug/unix/#s7"&gt;&lt;tt&gt;ktrace&lt;/tt&gt; or &lt;tt&gt;strace&lt;/tt&gt;&lt;/a&gt; on the process, and determine what—such as &lt;a href="http://www.freebsd.org/cgi/man.cgi?query=setenv
&amp;sektion=3"&gt;&lt;tt&gt;setenv(3)&lt;/tt&gt;&lt;/a&gt;, or the &lt;tt&gt;%ENV&lt;/tt&gt; hash in Perl&lt;/a&gt;—manipulate the evironment.&lt;/p&gt;




&lt;!-- technorati tags start --&gt;&lt;p style="text-align:right;font-size:10px;"&gt;Technorati Tags: &lt;a href="http://www.technorati.com/tag/Unix" rel="tag"&gt;Unix&lt;/a&gt;&lt;/p&gt;&lt;!-- technorati tags end --&gt;
&lt;br /&gt;

		</content>
		<issued xmlns="http://purl.org/atom/ns#">2007-11-13T10:44:28-0800</issued>
		<link xmlns="http://purl.org/atom/ns#" type="text/html" rel="alternate" href="http://sial.org/blog/2007/11/a_brief_primer_on_unix_environ.html" title=""/>
		<id xmlns="http://purl.org/atom/ns#">230</id>
	</entry>
</feed>
