Enviroment Variables#
Enviroment variables are used by the operationing system, programs and possibly your scripts. This variables can be set and access from the command line as well as in scripts/programs.
You can set enviroment variables:
export ENV_NAME=env_value
export ENV_NAME="env_value"
Example:
export MY_DATA=/users/username/datafiles
Viewing the contents of enviroment variables:
echo $ENV_NAME
Example:
> echo $MY_DATA
/users/username/datafiles
>
We cam also append and prepend to an enviromental variable:
export DATA=1,2,3,4
echo $DATA
1,2,3,4
export DATA=$DATA,5,6,7,8
echo $DATA
1,2,3,4,5,6,7,8
Combining environment variables and strings in scripts
By using curl braces, you can concatenate environment variables and strings. For example $MY_DATA becomes ${MY_DATA}:
#!/bin/bash
myprogram ${MY_DATA}/datafile1.dat
This would run the following command:
mprogram /users/username/datafiles/datafile1.dat
Passing variables from the command line to your script
You can pass variables from the command line to your script. Each option you specify after the script is accessible via an environment variables.
They are as follows:
$0 (This is the name of the script)
$1 (1st option)
$2 (2nd option)
and so on i.e. $3,$4,$5....
So the following:
myscript datafile1.dat 20
The variables are populated as:
$0 = myscript
$1 = datafile1.dat
$2 = 20
For example you might write your script so that you can specify which data file to use.:
myscript datafile1.dat
The script looks like this:
#!/bin/bash
myprogram $1
Which would produce:
myprgram datafile1.dat
General#
PATH
This is the path the OS will search to find programs
It will search from the first to last entry in order
We can prepend the path to one of our own programs:
export PATH=/dir/to/my/prog:$PATH
JAVA_HOME
The path to java
CFLAGS
C compiler flags
CXXFLAGS
C++ compiler Flags