Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Array Operations in Linux bash
Bash scripts are one of the most convenient approaches for automating command line processes. They help us perform multiple operations in a simpler and more understandable manner, allowing us to accomplish tasks similar to other programming languages. Arrays in bash provide a powerful way to store and manipulate collections of data elements.
The syntax of bash can be tricky at first, but this tutorial will explain the essential array operations step by step. We'll explore how to create, access, and manipulate arrays using practical examples that you can run directly in your terminal.
How to Create and Execute a Bash File
The first step when working with bash scripts is creating one. On Linux or macOS machines, create a bash file using the following command
touch mybashfile.sh
The filename can be anything you desire, but the .sh extension is conventional for shell scripts. Once you save the file, execute it with the following command
bash mybashfile.sh
Alternatively, you can make the file executable and run it directly
chmod +x mybashfile.sh ./mybashfile.sh
Creating Arrays in Bash
Bash arrays can be created in several ways. The most common method is using parentheses with space-separated elements
#!/bin/bash
# Method 1: Direct assignment
myarray=(apple banana mango kiwi litchi watermelon)
# Method 2: Individual element assignment
fruits[0]="apple"
fruits[1]="banana"
fruits[2]="mango"
# Method 3: Using declare
declare -a colors=("red" "green" "blue")
Accessing Array Elements
Printing the First Element
To access the first element of an array, use index 0
#!/bin/bash
myarray=(apple banana mango kiwi litchi watermelon)
# Printing the first element
echo ${myarray[0]}
echo "First fruit: ${myarray[0]}"
When you run this script, it produces the following output
apple First fruit: apple
Printing All Elements
Use [@] or [*] to access all array elements
#!/bin/bash
myarray=(apple banana mango kiwi litchi watermelon)
# Printing all array elements
echo "All fruits: ${myarray[@]}"
echo "Using asterisk: ${myarray[*]}"
Output
All fruits: apple banana mango kiwi litchi watermelon Using asterisk: apple banana mango kiwi litchi watermelon
Printing Elements in a Range
Use slice notation ${array[@]:start:length} to extract a range of elements
#!/bin/bash
myarray=(apple banana mango kiwi litchi watermelon)
# Printing array elements in specific ranges
echo "Elements 1-3: ${myarray[@]:1:3}"
echo "Elements 2-end: ${myarray[@]:2}"
echo "Last 2 elements: ${myarray[@]: -2}"
Output
Elements 1-3: banana mango kiwi Elements 2-end: mango kiwi litchi watermelon Last 2 elements: litchi watermelon
Array Properties and Operations
Getting Array Length
Use # operator to get the number of elements
#!/bin/bash
myarray=(apple banana mango kiwi litchi watermelon)
# Printing the number of elements
echo "Array length: ${#myarray[@]}"
echo "Alternative syntax: ${#myarray[*]}"
Output
Array length: 6 Alternative syntax: 6
Adding and Removing Elements
#!/bin/bash
fruits=(apple banana mango)
# Adding elements
fruits+=(kiwi litchi) # Append multiple elements
fruits[10]=watermelon # Add at specific index
# Removing elements
unset fruits[1] # Remove banana
unset fruits # Remove entire array
echo "Modified array: ${fruits[@]}"
echo "Array length: ${#fruits[@]}"
Practical Array Operations
| Operation | Syntax | Description |
|---|---|---|
| Access element | ${array[index]} | Get element at specific index |
| All elements | ${array[@]} or ${array[*]} | Get all array elements |
| Array length | ${#array[@]} | Get number of elements |
| Slice array | ${array[@]:start:length} | Get elements in range |
| Array indices | ${!array[@]} | Get all valid indices |
Iterating Through Arrays
#!/bin/bash
fruits=(apple banana mango kiwi)
# Method 1: Iterate over elements
for fruit in "${fruits[@]}"; do
echo "Fruit: $fruit"
done
# Method 2: Iterate using indices
for i in "${!fruits[@]}"; do
echo "fruits[$i] = ${fruits[i]}"
done
Conclusion
Bash arrays provide powerful capabilities for storing and manipulating collections of data. Understanding array operations like accessing elements, slicing, and iteration is essential for effective bash scripting. These operations form the foundation for more complex data processing tasks in shell scripts.
