Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Bash Arrays - DesignLinux

Nov 20 2020
designlinux 0 Comments
Bash Arrays

Arrays are set of data and fundamental requirement for any programming language. We can say that it’s a variable with multiple child variables. This article shows you basics and use of arrays in Bash scripts.

Bash Arrays#

One dimensional array with numbered index and associative array types supported in Bash. Those are referenced using integers and associative are referenced using strings. Bash does not support multidimensional arrays

To access the last element of a numeral indexed array use the negative indices. The index of -1 references the last element. The indices do not have to be contiguous.

In bash, array elements can any of data type. You also can create an array that have both numbers and strings. You can store any number of element in array, as there is not maximum limit of elements.

Create Bash Arrays#

In bash, you can create arrays with multiple ways.

Create numerically indexed arrays#

You can create indexed array without declaring it using any variable. To explicitly declare an array, use the declare builtin:

declare -a array_name

Following is the first method to create an indexed array:

array_name[INX_1]=VAL_1
array_name[INX_2]=VAL_2
array_name[INX_3]=VAL_3

Here, INX_* is a positive integer.

The second method to create a numeric array by specify the list of the elements within parentheses, separated by empty space:

array_name=( ELE_1 ELE_2 ELE_N )

In this method the index of an array starts with zero that means the first element have an index of 0.

Create associative arrays#

You must declare the associative array before they can be used. Use the built-in with the -A (uppercase) option to declare an associative array :

declare -A array_name

You can create the associative array using the following form:

declare -A array_name

array_name[inx_a]=value_a
array_name[inx_b]=value_b
array_name[inx_c]=value_c

Where inx_* can be any string.

Following is an alternate form to create an associative array:

declare -A array_name

array_name=( 
	[inx_a]=value_a
	[inx_b]=value_b
	[inx_c]=value_c
)

Array Operations#

The syntax for the bash looks little bit odd than other programming language.

Reference Elements#

Same as other language, to reference a single element, you should have the element index.

Any element can be referenced using the following syntax:

${array_name[index]}

For example, to print the element with index of 2:

declare -a state_array=( "California" "Texas" "Ohio" "Nevada" )
echo ${state_array[2]}
Ohio

To print the all elements of an array you would use @ or * as an index. Following form can be used to print all elements:

## DECLARATION OF ARRAY
declare -a state_array=( "California" "Texas" "Ohio" "Nevada" )

## PRINT ALL ELEMENTS
echo "${state_array[@]}"
California Texas Ohio Nevada

If you need to print keys of an array just add the ! operator before the array name:

${!array_name[index]}

Below is the example:

declare -a state_array=( "California" "Texas" "Ohio" "Nevada" )
print all elements
echo "${!state_array[@]}"
0 1 2 3

Array Length#

To get the length of an array, use the following form:

${#array_name[@]}
declare -a state_array=( "California" "Texas" "Ohio" "Nevada" )

echo ${#state_array[@]}
4

Loop through the array#

You can iterate over each item in an array using the for loop:

declare -a state_array=( "California" "Texas" "Ohio" "Nevada" )

## Array Loop
for i in "${state_array[@]}"
do 
  echo "$i"
done

It will print each element in a new line:

California
Texas
Ohio
Nevada

To print all keys and values using following code:

declare -a state_array=( "California" "Texas" "Ohio" "Nevada" )

for i in "${!state_array[@]}"
do
  echo "$i" "${state_array[$i]}"
done
0 California
1 Texas
2 Ohio
3 Nevada

Alternate way to loop an array using C style loop:

declare -a state_array=( "California" "Texas" "Ohio" "Nevada" )

length=${#my_array[@]}

for (( i=0; i < ${length}; i++ ))
do
  echo $i ${my_array[$i]}
done
0 California
1 Texas
2 Ohio
3 Nevada

Add a new element#

You should use the following form to add a new element to a bash array by specifying its index:

array_name[index_n]="New Element"

For example:

declare -a state_array=( "California" "Texas" "Ohio" "Nevada" )

## ADDING NEW ELEMENT
state_array[7]="Arkansas"

## PRINT ALL ELEMENTS
echo "${state_array[@]}"
California Texas Ohio Nevada Arkansas

You can add a new element without specifying the index by using the += operator.

declare -a state_array=( "California" "Texas" "Ohio" "Nevada" )

## ADDING NEW ELEMENT
state_array+=( Arizona Colorado )

## PRINT ALL ELEMENTS
echo "${state_array[@]}"
California Texas Ohio Nevada Arizona Colorado

Delete an element#

You can use the unset command to remove a element from an array. You should have the index of an element to delete it.

unset my_array[index]

For instance,

declare -a state_array=( "California" "Texas" "Ohio" "Nevada" )

unset state_array[2]

echo "${state_array[@]}"
California Texas Nevada

Conclusion#

In this guide, you learned how to create an array, get array length and add or remove elements from array.

If you have any questions or feedback, please leave comment below.

Related

Tags: bash, terminal

A Basic Guide to Linux Boot Process

Prev Post

How to Check if a String Contains a Substring in Bash

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 3
  • 603
  • 1,055,375

DesignLinux.com © All rights reserved

Go to mobile version