General Assignment and Declaration
[bob in ~] VARIABLE=12
[bob in ~] echo $VARIABLE
12
[bob in ~] VARIABLE=string
[bob in ~] echo $VARIABLE
string
Using a declare statement, we can limit the value assign to variables.
[bob in ~] declare -i VARIABLE=12
[bob in ~] VARIABLE=string
[bob in ~] echo $VARIABLE
0
[bob in ~] declare -p VARIABLE
declare -i VARIABLE="0"
[bob in ~] declare -i VARIABLE=12 [bob in ~] VARIABLE=string [bob in ~] echo $VARIABLE 0 [bob in ~] declare -p VARIABLE declare -i VARIABLE="0"
Constant
[bob in ~] readonly TUX=penguinpower
[bob in ~] TUX=Mickeysoft
bash: TUX: readonly variable
[bob in ~] readonly TUX=penguinpower [bob in ~] TUX=Mickeysoft bash: TUX: readonly variable
Array Variables
Access the Values of the Array
bob in ~] ARRAY=(one two three)
[bob in ~] echo ${ARRAY[*]}
one two three
[bob in ~] echo $ARRAY[*]
one[*]
[bob in ~] echo ${ARRAY[2]}
three
[bob in ~] ARRAY[3]=four
[bob in ~] echo ${ARRAY[*]}
one two three four
bob in ~] ARRAY=(one two three) [bob in ~] echo ${ARRAY[*]} one two three [bob in ~] echo $ARRAY[*] one[*] [bob in ~] echo ${ARRAY[2]} three [bob in ~] ARRAY[3]=four [bob in ~] echo ${ARRAY[*]} one two three four
[bob in ~] echo ${ARRAY[@]}
one two three four
[bob in ~] echo ${ARRAY[@]}
one two three four
Deleting Array
[bob in ~] unset ARRAY[1]
[bob in ~] echo ${ARRAY[*]}
one three four
[bob in ~] unset ARRAY
[bob in ~] echo ${ARRAY[*]}
<--no output-->
[bob in ~] unset ARRAY[1] [bob in ~] echo ${ARRAY[*]} one three four [bob in ~] unset ARRAY [bob in ~] echo ${ARRAY[*]} <--no output-->
Length of Array
[root@localhost ~]# ary1=(a b c d)[root@localhost ~]# echo ${#ary1[@]}
4
[root@localhost ~]# ary1[4]=e
[root@localhost ~]# echo ${ary1[@]}
a b c d e
[root@localhost ~]# echo ${#ary1[@]}
5
Looping through the Array
Put the content of a file in an array
a=( $( cat /tmp/ReqUrl.txt ) );for i in ${a[@]};
do
echo ${i}
done