In Bash, the hash table is also known as an associative array. It enables the user to store the values of arrays in the specified key name. It comes in handy when the user is dealing with an array having large and difficulty finding the index numbers of the record.
This post will cover how to define the hash table in Bash script with the following outline.
- Define Hash Tables in Bash
- Create Employee Information (Predefined Values)
- Authors List from User Input (User Input)
How to Define Hash Tables in Bash?
As mentioned earlier, the hash table is the associative array. The syntax for using the hash table is defined below:
Syntax:
declare -A <Array> #Define Array<Array>([Key]=Value) #Initialize Array
The syntax is defined as below:
- Use the “declare” keyword to define the name of the associative array (hash table) in <Array> using the “A” flag.
- The expression “<Array>([Key]=Value)” initializes the array with the name of the key index.
Example 1: Create Employee Information (Predefined Values)
The following table keeps the details of the employee at the specified key index:
#!/bin/bash
declare -A Emp_Details
Emp_Details=([name]="Henry" [company]="itslinuxfoss" [job]="Linux Expert")
echo "Name of the Employee is: ${Emp_Details[name]}"
echo "Company Name is: ${Emp_Details[company]}"
echo "Job is: ${Emp_Details[job]}"
The script is defined as below:
- First, the hash table “Emp_Details” is declared.
- Then the “Emp_Details” is initialized with employee details at the “name”, “company” and “job” indexes.
- The echo command prints the name of the employee at the specified index “${Emp_Details[name]}”.
- The echo command prints the company name for the employee at the specified index “${Emp_Details[company]}”
- Another echo command prints the job of the employee at the specified index “${Emp_Details[job]}”.
Run the bash command to execute the script file:
$ bash script.sh
The employee details including name, company, and job are listed as shown above.
Example 2: Authors List from User Input (User Input)
The following script will take input from the user to generate the author’s list:
#!/bin/bash
read -p "Enter First Author Name: " author1
read -p "Enter Second Author Name: " author2
read -p "Enter third Author Name: " author3
declare -A Authors_list
Authors_list=([name1]=$author1 [name2]=$author2 [name3]=$author3)
tput bold
echo "Name of the First Author is: ${Authors_list[name1]}"
echo "Name of the Second Author is: ${Authors_list[name2]}"
echo "Name of the Third Author is: ${Authors_list[name3]}"
The script is defined as
- The “read -p” property gets the name of the authors and stores it in the “author1”, “author2” and “author3” variable.
- The “declare” command is used to declare the hash table named “Authors_list”.
- The “Authors_list” is initialized with key indexes and “name1”, “name2” and “name3” with the user input stored in the variable.
- The “tput bold” is used to bold the below information.
- The echo command prints the first, second, and third author names at the specified indexes “${Authors_list[name1]}”, “${Authors_list[name2]}” and “${Authors_list[name3]}”.
Apply the bash command on the script file to get the result:
$ bash script.sh
The user entered the author’s names “Henry”, “Milton ” and “Joseph” and the list of authors has been generated accordingly.
Conclusion
In bash, the hash table is the associative array which is defined using the declare command along with the “A” flag. The syntax to define the hash tables in bash is “declare -A <Array>” and the array is then initialized with the syntax “<Array>([Key]=Value)”.
This write-up has illustrated the examples to define hash tables in Bash script.