#!/bin/bash

# The shell does not care about types of variables.
# The content is stored as a string of bytes regardless of what it is.
var_a="this is a string"
var_b=1
var_c=8.53
var_d="8.53"

# But certain operators expect an integer and treat the value as such.
# Observe the output of the next three commands.
# (We will discuss expr later.)
expr $var_b + 1
expr $var_d + 1
expr $var_a + 1

# The declare built-in can be used to assign attributes to variables.
# (Attributes are not exactly types in the formal sense.)
declare -i my_int_var    # variable is treated as an integer
declare -r my_ro_var     # variable is a constant. Also: readonly VAR=foo.
declare -a my_array      # variable is treated as an indexed array
declare -A my_assocarr   # variable is treated aa an associative array
declare -x my_export     # exports the variable via the environment
declare -l my_low_var    # always converts content to lowercase
declare -i my_up_var     # always converts content to uppercase
declare -g my_glob_var   # global var (only makes sense within a function)

# One useful option is -n, which makes the variable a reference to the
# variable named by its value.
foo=bar
declare -n myvar
myvar=foo
echo $foo
foo=bar2
echo $myvar

# Use declare without options to list all variables and their values in the
# current shell.
declare

# declare -p lists also the attributes.
declare -p

# Using '+' instead of '-' turns the attribute off.
declare +n myvar
echo $myvar

