Table of contents

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.

  1. Some types of variables

    There are two types of variables that are defined in bash script.

    • User-defined variables


      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
        # display a value of HOME variable
        printenv HOME
  2. Variable scope

    • Accessing a variable in two script files

        # define
        export name
        ./    # call the script2 to work
        # define
        echo $name

Declaration of an array

  1. 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=(

    The starting index of an array is 0.

  2. Some useful operations

    • Get the length of an array

    • 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[@]}"
            echo "$i"
        # 2nd way
        for (( idx=1; idx<${count}+1; idx++ ));
            echo $idx " / " ${count} " : " ${arr[$idx-1]}

Declaration of an associative data structure

  1. Syntax

     # declaration without initialization
     declare -A map
     echo ${map[foo]}
     # declaration with initialization
     declare -A map=( [foo]=bar [baz]=google )
     echo ${map[foo]}
  2. Some useful operations

    • loop all keys

        for key in "${!map[@]}"; do
            echo $key
    • loop all values

        for value in "${map[@]}"; do
            echo $value
    • loop all keys and values

        for key in "${!map[@]}"; do
            echo "$key --- ${map[$key]}"
    • 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

        echo ${keys[0]}
        for (( idx=0; idx < ${#map[@]}; idx++ )); do
            echo "$key --- ${map[$key]}"

Some useful scripts

  1. Receive input from user

     # 1st way - using read function and $REPLY variable
     echo -n "What is your name?: "
     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
  2. Validation with the number of parameter in function

     [ $# -gt 0]

Wrapping up

  • Understanding about variables in shell script.