You can use variables to store data and configuration options. There are two types of variable as follows
- System Variables
- User Defined Variables
Created and maintained by Linux bash shell itself. This type of variable (with the exception of auto_resume and histchars) is defined in CAPITAL LETTERS. You can configure aspects of the shell by modifying system variables such as PS1, PATH, LANG,HISTSIZE,and DISPLAY etc.
To see all system variables, type the following command at a console / terminal:
set
env
printenv| System Variable | Meaning | To View Variable Value Type |
|---|---|---|
| BASH_VERSION | Holds the version of this instance of bash. | echo $BASH_VERSION |
| HOSTNAME | The name of the your computer. | echo $HOSTNAME |
| CDPATH | The search path for the cd command. | echo $CDPATH |
| HISTFILE | The name of the file in which command history is saved. | echo $HISTFILE |
| HISTFILESIZE | The maximum number of lines contained in the history file. | echo $HISTFILESIZE |
| HISTSIZE | The number of commands to remember in the command history. The default value is 500. | echo $HISTSIZE |
| HOME | The home directory of the current user. | echo $HOME |
| IFS | The Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read builtin command. The default value is | echo $IFS |
| LANG | Used to determine the locale category for any category not specifically selected with a variable starting with LC_. | echo $LANG |
| PATH | The search path for commands. It is a colon-separated list of directories in which the shell looks for commands. | echo $PATH |
| PS1 | Your prompt settings. | echo $PS1 |
| TMOUT | The default timeout for the read builtin command. Also in an interactive shell, the value is interpreted as the number of seconds to wait for input after issuing the command. If not input provided it will logout user. | echo $TMOUT |
| TERM | Your login terminal type. | echo $TERM ; export TERM=vt100 |
| SHELL | Set path to login shell. | echo $SHELL |
| DISPLAY | Set X display name | echo $DISPLAY; export DISPLAY=:0.1 |
| EDITOR | Set name of default text editor. | export EDITOR=/usr/bin/vim |
Use echo command to display variable value. To display the program search path
echo $PATH
# Or
echo ${PATH}Created and maintained by user. This type of variable defined may use any valid variable name, but it is good practice to avoid all uppercase names as many are used by the shell.
Creating and setting variables within a script is fairly simple. Use the following syntax
varName=someValueStore current date (you can store the output of date by running the shell command):
now=$(date)You can set the default shell variable value using the following syntax.
${var:=defaultValue}
# Or
${var:-defaultValue}Example
die(){
local error=${1:-Undefined error}
echo "$0: $error"
}
die "File not found"
die- Variable name must begin with alphanumeric character or underscore character (_), followed by one or more alphanumeric or underscore characters.
- Do not use ?,* and other special characters, to name your variable.
- Variables names are case-sensitive
- Variables defined a NULL variable as follows (NULL variable is variable which has no value at the time of definition)
Valid shell variable examples:
HOME
SYSTEM_VERSION
vech
noDo not put spaces on either side of the equal sign when assigning value to variable. For example, the following is valid variable declaration:
var=10However, any of the following variable declaration will result into an error such as command not found:
var =10
var= 10
var = 10Not initialized or NULL variable
var=
var=''The export builtin automatically exports to the environment of child processes. By default all user defined variables are local. They are not exported to new processes. Use export command to export variables and functions to child processes. If no variable names or function names are given, or if the -p option is given, a list of all names that are exported in this shell is printed. An argument of -n says to remove the export property from subsequent NAMEs
export backup="/nas10/mysql"
echo "Backup dir $backup"Use unset command to delete the variables during program execution. It can remove both functions and shell variables.
var=/etc/profile
echo $var
unset var
echo $var$((expression))
$(( n1+n2 ))
$(( n1/n2 ))
$(( n1-n2 ))| Operator | Description | Example | Evaluates To |
|---|---|---|---|
| + | Addition | $(( 20 + 5 )) | |
| - | Substraction | $(( 20 - 5 )) | |
| / | Divison | $(( x / 2 )) | |
| % | Modules | $(( x % 10 )) | |
| * | Multipilication | $(( x * y )) | |
| ++ | post-increment | $(( x++ )) | |
| -- | post-decrement | $(( x-- )) | |
| ** | Exponentiation | $(( x ** y )) |
To create an integer variable use the declare command as follows
declare -i x=10
declare -i y=10
declare -i z=0
z=$(( x + y ))
echo "$x + $y = $z" You can create the constants variables using the readonly command or declare command Syntax
readonly var
readonly varName=value
declare -r var
declare -r varName=valueExample
readonly DATA=/home/sales/data/feb09.datIf the variable is not defined, you can stop executing the Bash script with the following syntax
${varName?Error varName is not defined}
${varName:?Error varName is not defined or is empty}Example
#!/bin/bash
# varcheck.sh: Variable sanity check with :?
path=${1:?Error command line argument not passed}
echo "Backup path is $path."
echo "I'm done if \$path is set."We can explicitly create an array by using the declare command
declare -a my_arrayWe can create indexed arrays with a more concise syntax, by simply assign them some values
my_array=(foo bar)In this case we assigned multiple items at once to the array, but we can also insert one value at a time, specifying its index
my_array[0]=foo${array[$i])echo ${my_array[@]}
# Or
echo ${my_array[*]}
# Or
for i in "${my_array[@]}"; do echo "$i"; done
# Or
for i in ${#my_array[@]}; do echo ${array[$i]); done When using *, and the variable is quoted, instead, a single “result” will be produced, containing all the elements of the array
It’s even possible to retrieve and print the keys used in an indexed or associative array, instead of their respective values. The syntax is almost identical, but relies on the use of the ! operator
my_array=(foo bar baz)
for index in "${!my_array[@]}"; do echo "$index"; done${!variable[@]}Example
declare -A my_array
my_array=([foo]=bar [baz]=foobar)
for key in "${!my_array[@]}"; do echo "$key"; done${#variable[@]}Example
my_array=(foo bar baz)
echo "the array size : ${#my_array[@]}"
$ the array size : 3variable+=(value)Example
my_array=(foo bar)
my_array+=(baz)
# Or
my_array=(foo bar)
my_array+=(baz foobar)To add elements to an associative array, we are bound to specify also their associated keys:
declare -A my_array
my_array[foo]="bar"
my_array+=([baz]=foobar [foobarbaz]=baz)To delete an element from the array we need to know it’s index or its key in the case of an associative array, and use the unset command
Example
my_array=(foo bar baz)
unset my_array[1]
echo ${my_array[@]}
foo bazThe same thing it’s valid for associative arrays:
declare -A my_array
my_array+=([foo]=bar [baz]=foobar)
unset my_array[foo]
echo ${my_array[@]}
foobar