The Shell Environment
What is the environment?
The unix/linux environment consists of a set of shell environmental variables: some set by the operating system, some by the shell, some by the user and some by applications or scripts. While each shell may have different methods to change the environment, bash is the most widely used shell and this page will focus on bash exclusively.
http://www.tutorialspoint.com/unix/unix-environment.htm
To show the entire environment with this command:
env
Understanding what all entries mean with take some time. Each entry is a variable so lets understand the basics of shell variables. This next section is about how variables are handled in the bash shell. There are other shells that can be used in Linux, they handle variables differently, but he concepts are the same.
Bash variables
The bash scripts frequently use variables to simply those scripts and make it easier to change.
Basics
To set a variable in bash do the following:
<variable name>=<setting>
make sure there are no spaces on either side of the equals. The variable is case sensitive, but all capitals is frequently used to denote important varibles.
An example:
FOO=bar
To display the contents of a variable, the following command is the easiest way. A '$' is placed in front of the variable name.
echo $FOO
The output will be "bar"
It can also be written ${FOO} so that a script can contain a variable without whitespace to the next statement, like so:
echo "pick the ${COUNT}th item"
export a variable
The export command means that the variable is not only available to this shell, but to all shells that are spawned by this shell, also called child processes
Example:
$ FOO=bar
$ sh -c 'echo $FOO'
$ export FOO=barnone
$ sh -c 'echo $FOO'
bar
https://stackoverflow.com/questions/7328223/unix-export-command
The output of commands used as a variable.
Variables can also contain be the output of commands:
An example: create a file called date.sh with the following:
#!/bin/env bash
THEDATE=$(date +%F)
echo $THEDATE
If you read older scripts it may use backticks for the the same purpose. This is now deprecated, use the $( ) method instead.
DATE=`date +%F`
Sourcing a file
The statement: source <file> is often seen in scripts as a means to incorporate the contents of one file into another. The file that is sourced frequently contains variables such as PATH and bash functions. While files could be executed
For example, create a file that contains a varible:
echo "FOO=barbar" > foo.sh
and another file that sources the previous file and then echos the contents of the variable.
echo "echo $FOO" > bar.sh
echo "source ./foo.sh" >> bar.sh
echo "echo $FOO" >> bar.sh
Then run it
./bar.sh
An equivalent to the "source" statement is to use a dot: . <file> But I think using "source" is more readable.
What is the path?
The path is one of the most used variables in the environment. The path is an ordered list of directories to search for when executable is requested to run.
The "which" command will display the absolute directory for a given command. But if no directory is returned, then the given command does not exist in the PATH
example:
which ls
returns /bin/ls
but
which la
returns no la found in PATH
Show the contents of the PATH environmental variable with this command:
echo $PATH
A simple way to append path
export PATH=/additional/path:$PATH
The path is usually appended to the front of the PATH. Separate directories must be separated by a colon. There is no space around the = and the must be a $ in front of the final PATH, but not the first PATH
more advanced way to append the path:
export PATH=/additional/path${PATH:+:${PATH}}
Note: no colon after the last new path
why? see https://unix.stackexchange.com/questions/267506/what-does-pathpath-mean/267548
How to determine if a given command in the path? Use the which command:
which <command>
also multiple commands:
which <command1> <command2> <command3>
Example: Append the path to include the blastx application that is located in the directory /usr/local/ncbi-blast-2.6.0+/bin
Show original path:
echo $PATH
Is the blastx application in this path?
which blastx
Append path
export PATH=/usr/local/ncbi-blast-2.6.0+/bin:$PATH
Show path again
echo $PATH
Try which again
which blastx
Show version of blastx
blastx -version