<?xml version="1.0"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
<article id="UnixEnvars">
  <articleinfo>
				<title>Configuring A Unix Working Environment</title>
    <author>
      <firstname>Ashley</firstname>
      <othername>J.S</othername>
      <surname>Mills</surname>
      <affiliation>
        <address format="linespecific"><email>ashley@ashleymills.com</email></address>
      </affiliation>
    </author>

    <copyright>
      <year>2005</year>
      <holder role="mailto:ashley@ashleymills.com">The University Of Birmingham</holder>
    </copyright>
  </articleinfo>

		<sect1 id="UnixEnvars-Introduction"><title>Introduction To The Unix Environment</title>
				<para>
						When a Unix system starts up a series of scripts are run which set the operating system up, eventually the user will be presented with the familiar login prompt or equivalent.  Upon logging in some script(s) are executed which set up the working environment.  The particular script(s) executed will depend on the default shell.
				</para>

    <para>
						A Unix shell is an interface for controlling a Unix system.  There are quite a few different shells out there, some of them have similar syntax', some of them vary in their syntax'.  The most commonly used Unix shells are <acronym>SH</acronym>(Bourne SHell), <acronym>CSH</acronym>(C SHell), <acronym>RSH</acronym>(Restricted SHell) and <acronym>KSH</acronym>(Korn SHell).  It is very likely that at least one of these shells will be available on the Unix system you are using. There are also a number of other shells available to the Unix user such as <acronym>BASH</acronym>(Bourne Again SHell), <acronym>TCSH</acronym>(Enhanced C SHell) and <acronym>ZSH</acronym>(Z SHell). 
				</para>

				<para>
						I am not going to go into depth about the various merits of using one shell over another, this is already covered in detail at <ulink url="http://www.nscp.umd.edu/shells.html">http://www.nscp.umd.edu/shells.html</ulink> or alternatively in the <acronym>FAQ</acronym> posted to the newsgroup <ulink url="news:comp.unix.shell">comp.unix.shell</ulink> monthly, also a place where you can discuss the idiosyncrasies of Unix shellism to your hearts content.
				</para>

				<para>
						You can find out what shell you are using by issuing the commands <screen><userinput>echo $SHELL</userinput></screen>.  If you are using <acronym>CSH</acronym> or some variant the scripts executed at login will be <filename>.login</filename> and <filename>.cshrc</filename> these are situated in the users home directory described by the <envar>HOME</envar> environment variable which will be explained later.  If you are using <acronym>SH</acronym> or some variant the script executed at login will be <filename>.profile</filename>.  Any settings in this/these file(s) will effect the user whom owns them.  The file(s) is/are used to setup the working environment for the user who owns them. If you start a new shell from within another shell, the new shells startup files will be executed. In addition, if you are using <acronym>CSH</acronym> <filename>.cshrc</filename> will be executed every time a further instance of <acronym>CSH</acronym> is invoked.
				</para>

				<para>
						The environment contains variables called environment variables which contain information that is useful or essential for the Unix operating system to function as the user expects it to.  An example environment variable is the <envar>PATH</envar> environment variable which contains a colon separated list of directories indicating where the Unix operating system expects executable programs to be found. When a command is typed into the shell, the Unix operating system will search through the directories listed in the <envar>PATH</envar> environment variable in the order they occur until it either finds an executable matching the command typed or fails to find a match. A few other examples of environment variables found on the Unix operating system are listed below:
				</para>

				<itemizedlist>
						<listitem>
								<para>
										<envar>EDITOR</envar>: Specifies the default editor.
								</para>
						</listitem>
						<listitem>
								<para>
										<envar>PWD</envar>: Specifies the current working directory.
								</para>
						</listitem>
						<listitem>
								<para>
										<envar>USER</envar>: Specifies the current user.
								</para>
						</listitem>
						<listitem>
								<para>
										<envar>SHELL</envar>: Specifies the default shell.
								</para>
						</listitem>
						<listitem>
								<para>
										<envar>RANDOM</envar>: Contains a random integer which changes each time the variable is referenced.
								</para>
						</listitem>
				</itemizedlist>

				<note>
						<para><acronym>TCSH</acronym> uses the configuration file <filename>.tcshrc</filename> instead of <filename>.cshrc</filename></para>
				</note>
		</sect1>

		<sect1 id="UnixEnvars-ChangingThem"><title>Altering Environment Variables</title>
				<sect2 id="UnixEnvar-ChangingThem-CSH"><title><acronym>CSH</acronym></title>
				  <para>
        If you are using <acronym>CSH</acronym> or some variant you can change environment variables temporarily from the command line using the following syntax:
				  </para>

				  <screen><userinput><command>setenv</command> <replaceable>VARIABLENAME</replaceable> <replaceable>value_of_variable</replaceable></userinput></screen>

      <para>
					  	The changes will remain in effect until you logout or invoke a new shell. If you would like to change an environment variable permanently you should edit the file <filename>.cshrc</filename> and insert the line:
				  </para>

				  <programlisting>
<command>setenv</command> <replaceable>VARIABLENAME</replaceable> <replaceable>value_of_variable</replaceable>
      </programlisting>

	  			<para>
								The changes will come into effect the next time you login or invoke an instance of <acronym>CSH</acronym> if you cannot wait that long, you can type <screen><userinput><command>source</command> <filename>.cshrc</filename></userinput></screen> to put the changes into effect immediately. It can be useful to append to the end of an environment variable, this can be achieved using the following syntax at the command line and within <filename>.cshrc</filename>:
			  	</para>

						<programlisting>
<command>setenv</command> <replaceable>VARIABLE</replaceable> ${<replaceable>VARIABLE</replaceable>}:<filename>value_to_append</filename>
      </programlisting>

						<para>
								This will append <emphasis role="strong">:value_to_append</emphasis> to the end of the current value of the variable.  In the example above the character <emphasis role="strong">':'</emphasis> has been placed between <emphasis role="strong">${VARIABLE}</emphasis> and <emphasis role="strong">value_to_append</emphasis>, this is not mandatory but has been included to illustrate that this type of setting of environment variables which appends to the variable is often used to add a directory to a list of directories or a file to a list of files, when this is the case <emphasis role="strong">':'</emphasis> is the standard separator used.
						</para>
				</sect2>

				<sect2 id="UnixEnvar-ChangingThem-SH"><title><acronym>SH</acronym></title>

				  <para>
        If you are using <acronym>SH</acronym> or some variant you can change environment variables temporarily from the command line using the following syntax:
				  </para>

						<screen><userinput><command>export</command> <replaceable>VARIABLENAME</replaceable>=<replaceable>value_of_variable</replaceable></userinput></screen>

      <para>
								The changes will remain in effect until you logout or invoke a new shell. If you would like to change an environment variable permanently you should edit the file <filename>.profile</filename> and insert the line:
				  </para>

						<programlisting>
<replaceable>VARIABLE</replaceable>=<replaceable>value_of_variable</replaceable>
<command>EXPORT</command> <replaceable>VARIABLE</replaceable> 
				  </programlisting>

	  			<para>
								The changes will come into effect the next time you login or invoke an instance of <acronym>SH</acronym> if you cannot wait that long, you can type <screen><userinput><command>.</command> <filename>.profile</filename></userinput></screen> to put the changes into effect immediately. It can be useful to append to the end of an environment variable, this can be achieved using the following syntax from the command line:
			  	</para>

						<programlisting>
<command>EXPORT</command> <replaceable>VARIABLE</replaceable>=$<replaceable>VARIABLE</replaceable>:<replaceable>value_to_append</replaceable>	
						</programlisting>

						<para>And from within <filename>.profile</filename>:</para>

      <programlisting>
<replaceable>VARIABLE</replaceable>=$<replaceable>VARIABLE</replaceable>:<replaceable>value_to_append</replaceable>
<command>EXPORT</command> <replaceable>VARIABLE</replaceable>
    </programlisting>

						<para>
								This will append <emphasis role="strong">:value_to_append</emphasis> to the end of the current value of the variable.  In the example above the character <emphasis role="strong">':'</emphasis> has been placed between <emphasis role="strong">${VARIABLE}</emphasis> and <emphasis role="strong">value_to_append</emphasis>, this is not mandatory but has been included to illustrate that this type of setting of environment variables which appends to the variable is often used to add a directory to a list of directories or a file to a list of files, when this is the case <emphasis role="strong">':'</emphasis> is the standard separator used.
						</para>
				</sect2>
		</sect1>

  <sect1 id="UnixEnvar-Installing-Software"><title>Installing Software</title>
    <para>There are generally two ways of installing software on Unix:</para>
    <orderedlist>
      <listitem>
        <para>
          Install from pre-packaged distribution dependent medium such as <acronym>RPM</acronym>, <acronym>PKG</acronym> or <acronym>BIN</acronym>.
        </para>
      </listitem>
      <listitem>
        <para>
          Compile and install from source files.
        </para>
      </listitem>
    </orderedlist>

    <sect2 id="UnixEnvar-Installing-Software-Packaged"><title>Installing From A Pre-packaged Medium</title>
      <para>
        <acronym>RPM</acronym> (<acronym>RPM</acronym> Package Manager) is an easy to use package management system under the <acronym>GPL</acronym> license. The homepage is <ulink url="http://www.rpm.org/">http://www.rpm.org/</ulink>. An <acronym>RPM</acronym> search engine can be found at <ulink url="http://rpmfind.net/">http://rpmfind.net/</ulink>. It is only necessary to know a few simple commands to use <acronym>RPM</acronym>:
      </para>

      <screen><userinput><command>rpm</command> <option>-i</option> <replaceable>somepackage.rpm</replaceable></userinput></screen>
      <para>Installs the package specified.</para>

      <screen><userinput><command>rpm</command> <option>-e</option> <replaceable>somepackage</replaceable></userinput></screen>

      <para>Removes the package specified.</para>

      <screen><userinput><command>rpm</command> <option>-i</option> <replaceable>ftp://blah/blah/blah/somepackage.rpm</replaceable></userinput></screen>

      <para>Installs an <acronym>RPM</acronym> from some URL.</para>

      <screen><userinput><command>rpm</command> <option>--help</option></userinput></screen>

      <para>Displays the builtin help.</para>

      <para>
        Additional help for installing <acronym>RPM</acronym>s can be found at <ulink url="http://www.rpm.org/RPM-HOWTO/">http://www.rpm.org/RPM-HOWTO/</ulink>. If you are using <ulink url="http://www.freebsd.org/">FreeBSD</ulink>, you can benefit from using the ports system, explained at <ulink url="http://www.freebsd.org/ports/index.html">http://www.freebsd.org/ports/index.html</ulink>.
      </para>
    </sect2>

    <sect2 id="UnixEnvar-Installing-Software-Sources"><title>Installing From Sources</title>
      <para>
        Occasionally a program may only be available in source format, or you may desire to have the very latest version, which has not yet been packaged, if this is the case you will have to install from the sources. Source code usually comes in a gzipped tar archive (<filename>*.tar.gz</filename>), this will need to be extracted to a temporary directory somewhere:
      </para>

      <screen>
        <userinput><command>tar</command> <option>xzvf</option> <replaceable>blah.tar.gz</replaceable></userinput>
      </screen>

      <para>
        Will extract the gzipped tar archive to the current directory. <command>cd</command> to the directory created (something like <filename>blah-1.4.2</filename>). This directory should contain a file called <filename>INSTALL</filename> detailing how to compile and install the program, you should follow the instructions contained therein, generically one installs from source like this:
      </para>

      <screen><userinput><command>./configure</command></userinput></screen>

      <para>To configure the installation for your machine.</para>

      <screen><userinput><command>make</command></userinput></screen>

      <para>To build the binary files.</para>

      <screen><userinput><command>make</command> <option>install</option></userinput></screen>

      <para>To install the binaries in some suitable location, for example, <filename>/usr/local/bin/</filename>.</para>

      <note>
        <para>
          This procedure assumes a <emphasis role="strong">c</emphasis>-based program, increasingly, programs are being made entirely in Java and so this installation procedure does not apply. Instead, follow the instructions that come with the downloaded program.
        </para>
      </note>
    </sect2>
  </sect1>
</article>

