
- Kali Linux Tutorial
- Kali Linux - Home
- Installation & Configuration
- Information Gathering Tools
- Vulnerability Analyses Tools
- Kali Linux - Wireless Attacks
- Website Penetration Testing
- Kali Linux - Exploitation Tools
- Kali Linux - Forensics Tools
- Kali Linux - Social Engineering
- Kali Linux - Stressing Tools
- Kali Linux - Sniffing & Spoofing
- Kali Linux - Password Cracking Tools
- Kali Linux - Maintaining Access
- Kali Linux - Reverse Engineering
- Kali Linux - Reporting Tools
- Kali Linux Useful Resources
- Kali Linux - Quick Guide
- Kali Linux - Useful Resources
- Kali Linux - Discussion
Introduction to Bash Array in Linux
Introduction
Bash is a popular command-line shell used in Linux and other Unix-based operating systems. One of powerful features of Bash is its support for arrays. An array is a collection of values that can be accessed using an index. In this article, we will discuss basics of Bash arrays and how they can be used in Linux.
What is a Bash Array?
A Bash array is a variable that can hold multiple values. values are stored in consecutive memory locations and can be accessed using an index. index of first element in array is 0, and index of last element is total number of elements minus one.
Declaring an Array
To declare an array in Bash, you use following syntax −
array_name=(value1 value2 value3 …)
For example, to declare an array of fruit names, you can use following command −
fruits=(apple banana cherry)
You can also declare an array with values on separate lines −
fruits=( apple banana cherry )
Accessing Array Elements
To access an element in an array, you use following syntax −
${array_name[index]}
For example, to access first element of fruits array, you can use following command −
echo ${fruits[0]}
This will output apple.
You can also use variables as indices to access elements in an array −
index=1 echo ${fruits[$index]}
This will output banana.
Array Length
To get length of an array, you can use following syntax −
${#array_name[@]}
For example, to get length of fruits array, you can use following command −
echo ${#fruits[@]}
This will output 3.
Iterating Over an Array
To iterate over an array, you can use a for loop. Here is an example −
for fruit in "${fruits[@]}" do echo $fruit done
This will output −
apple banana cherry
Appending to an Array
To append a value to an array, you can use following syntax −
array_name+=(value)
For example, to append "orange" to fruits array, you can use following command −
fruits+=(orange)
This will add "orange" to end of fruits array.
Removing an Element from an Array
To remove an element from an array, you can use unset command. Here is an example −
unset array_name[index]
For example, to remove second element from fruits array, you can use following command −
unset fruits[1]
This will remove "banana" from fruits array.
Multi-dimensional Arrays
Bash also supports multi-dimensional arrays. A multi-dimensional array is an array of arrays. Here is an example −
matrix=( (1 2 3) (4 5 6) (7 8 9) )
To access an element in a multi-dimensional array, you use following syntax −
${array_name[row_index][column_index]}
For example, to access element in second row and third column of matrix array, you can use following command −
echo ${matrix[1][2]}
This will output 6.
Additional Topics
Associative Arrays in Bash
In addition to indexed arrays we have discussed so far, Bash also supports associative arrays. An associative array is a collection of key-value pairs, where key is a unique identifier for a value. To declare an associative array, you use following syntax −
declare -A array_name
For example, to declare an associative array of fruit prices, you can use following command −
declare -A fruit_prices fruit_prices=([apple]=0.50 [banana]=0.25 [cherry]=0.10)
To access an element in an associative array, you use key instead of an index −
echo ${fruit_prices[apple]}
This will output 0.50.
You can also iterate over an associative array using a for loop −
for key in "${!fruit_prices[@]}" do echo "$key: ${fruit_prices[$key]}" done
This will output −
apple: 0.50 banana: 0.25 cherry: 0.10
Copying Arrays
To copy an array in Bash, you can use following syntax −
new_array=("${old_array[@]}")
For example, to copy fruits array, you can use following command −
new_fruits=("${fruits[@]}")
This will create a new array called new_fruits that contains same values as fruits array.
Sorting Arrays
Bash provides several ways to sort arrays. One way is to use sort command with -n option to sort array numerically, or -r option to sort array in reverse order. Here is an example −
sorted_fruits=($(echo "${fruits[@]}" | tr ' ' '
' | sort -r))
This will sort fruits array in reverse order and store result in sorted_fruits array.
Another way to sort arrays is to use a loop and compare each element with others. Here is an example −
for ((i=0; i<${#fruits[@]}; i++)) do for ((j=i+1; j<${#fruits[@]}; j++)) do if [[ "${fruits[i]}" > "${fruits[j]}" ]] then temp=${fruits[i]} fruits[i]=${fruits[j]} fruits[j]=$temp fi done done
This will sort fruits array in ascending order.
Using Arrays in Bash Scripts
Arrays can be very useful in Bash scripts. For example, you can use arrays to store command-line arguments, input from users, or results from commands. Here is an example Bash script that uses an array to store user input −
#!/bin/bash echo "Enter your favorite fruits, separated by spaces:" read -a fruits echo "Your favorite fruits are:" for fruit in "${fruits[@]}" do echo $fruit done
This script prompts user to enter their favorite fruits, stores input in an array called fruits, and then displays contents of array.
Conclusion
In this article, we have discussed basics of Bash arrays and how they can be used in Linux. We have covered declaring an array, accessing array elements, array length, iterating over an array, appending to an array, removing an element from an array, and multi-dimensional arrays. Bash arrays are a powerful feature that can simplify many tasks in Linux, such as data processing and management. By mastering Bash arrays, you can become more efficient in your Linux command-line work.
- Related Articles
- Introduction to Bash Globbing on Linux
- Array Operations in Linux bash
- Introduction to fzf command in Linux
- Introduction to tee Command in Linux
- Introduction to File Locking in Linux
- Bash Functions in Linux
- Bash Special Variables in Linux
- Escaping Characters in Bash on Linux
- String Manipulation in Bash on Linux
- Preventing Bash Fork Bombs in Linux
- Introduction to File MIME Types on Linux
- How to Clear BASH Command Line History in Linux?
- Testing Bash Scripts With Bats in Linux
- Difference Between sh and Bash in Linux?
- How to Create a New File in Linux from Bash?
