Local variables
Local variables are only available in the current shell. Using the set built-in command without any
options will display a list of all variables (including environment variables) and functions. The
output will be sorted according to the current locale and displayed in a reusable format.
Below is a diff file made by comparing printenv and set output, after leaving out the functions
which are also displayed by the set command:
franky ~> diff set.sorted printenv.sorted | grep "<" | awk '{ print $2 }'
BASE=/nethome/franky/.Shell/hq.garrels.be/octarine.aliases
BASH=/bin/bash
BASH_VERSINFO=([0]="2"
BASH_VERSION='2.05b.0(1)-release'
COLUMNS=80
DIRSTACK=()
DO_FORTUNE=
EUID=504
GROUPS=()
HERE=/home/franky
HISTFILE=/nethome/franky/.bash_history
HOSTTYPE=i686
IFS=$'
LINES=24
MACHTYPE=i686-pc-linux-gnu
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PIPESTATUS=([0]="0")
PPID=10099
PS4='+
PWD_REAL='pwd
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
THERE=/home/franky
UID=504
Variables by content
Apart from dividing variables in local and global variables, we can also divide them in categories
according to the sort of content the variable contains. In this respect, variables come in 4 types:
String variables
Integer variables
Constant variables
Array variables
Creating variables
Variables are case sensitive and capitalized by default. Giving local variables a lowercase name is a
convention which is sometimes applied. However, you are free to use the names you want or to mix
cases. Variables can also contain digits, but a name starting with a digit is not allowed:
prompt> export 1number=1
bash: export: `1number=1': not a valid identifier
To set a variable in the shell, use
VARNAME="value"
Putting spaces around the equal sign will cause errors. It is a good habit to quote content strings
when assigning values to variables: this will reduce the chance that you make errors.
Some examples using upper and lower cases, numbers and spaces:
franky ~> MYVAR1="2"
franky ~> echo $MYVAR1
2
franky ~> first_name="Franky"
franky ~> echo $first_name
Franky
franky ~> full_name="Franky M. Singh"
franky ~> echo $full_name
Franky M. Singh
franky ~> MYVAR-2="2"
bash: MYVAR-2=2: command not found
franky ~> MYVAR1 ="2"
bash: MYVAR1: command not found
franky ~> MYVAR1= "2"
bash: 2: command not found
franky ~> unset MYVAR1 first_name full_name
franky ~> echo $MYVAR1 $first_name $full_name
<--no output-->
franky ~>