Table of contents
- How to define variables
- Declaration of an array
- Declaration of an associative data structure
- Some useful objects
- Wrapping up
How to define variables
To understand the basic thing of variables in Linux, we can visit the article How to write simple shell scripts in Linux.
-
Some types of variables
There are two types of variables that are defined in bash script.
-
User-defined variables
path="./home/user" num=3
To declare a variable, typing its name and set its value by using equal sign. There are no spaces between the variable name and the equals sign, or between the equals sign and the value. If existing space, shell will consider the definition of variables as a command.
-
Environment variables
To see the definitions of environment variables, we can use printenv command.
# list all environment variables printenv # display a value of HOME variable printenv HOME
-
-
Variable scope
-
Accessing a variable in two script files
# define script1.sh #!/bin/bash name="google.com.vn" export name ./script2.sh # call the script2 to work # define script2.sh echo $name
-
Declaration of an array
-
Syntax
# 1st way - enclosing elements between brackets arr=(hello world what are you doing?) # 2nd way - using declare keyword declare -a arr=("hello" "world" "what" "are" "you" "doing?") # or declare an array in multiple lines declare -a arr=( "hello" "world" "what" "are" "you" "doing?" )
The starting index of an array is 0.
-
Some useful operations
-
Get the length of an array
count=${#arr[@]}
-
Print the array elements, using an asterisk
echo ${arr[*]}
-
Remove a specific element from an array
# remove all elements unset arr # remove the first element unset arr[0]
-
iterate all elements from an array
# 1st way for i in "${arr[@]}" do echo "$i" done # 2nd way count=${#arr[@]} for (( idx=1; idx<${count}+1; idx++ )); do echo $idx " / " ${count} " : " ${arr[$idx-1]} done
-
Declaration of an associative data structure
-
Syntax
# declaration without initialization declare -A map map[foo]=bar map[baz]=google echo ${map[foo]} # declaration with initialization declare -A map=( [foo]=bar [baz]=google ) echo ${map[foo]}
-
Some useful operations
-
loop all keys
for key in "${!map[@]}"; do echo $key done
-
loop all values
for value in "${map[@]}"; do echo $value done
-
loop all keys and values
for key in "${!map[@]}"; do echo "$key --- ${map[$key]}" done
-
remove a key
# remove this map unset map # remove a key unset map[foo]
-
show the length of this map
echo ${#map[@]}
-
get an array of keys
keys=("${!map[@]}") echo ${keys[0]} for (( idx=0; idx < ${#map[@]}; idx++ )); do key=${keys[$idx]} echo "$key --- ${map[$key]}" done
-
Some useful scripts
-
Receive input from user
# 1st way - using read function and $REPLY variable echo -n "What is your name?: " read echo "Hello $RERPLY" # 2nd way - using read function with -p option # The argument that follows the option is the text that appear in the prompt. # The last argument supplied is the variable we want to populate. # If we do not supply the last argument, we can store the user's response in $REPLY variable. read -p "What is your name: " name
-
Validation with the number of parameter in function
[ $# -gt 0]
Wrapping up
- Understanding about variables in shell script.
Refer:
http://tiebing.blogspot.com/2008/01/bash-programming-reference-card.html