codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐧

Linux & Bash

41 / 60 topics
39Advanced Scripting40Bash Arrays41Bash Associative Arrays42Advanced Functions43Advanced Script Debugging44Script Optimization45Automation with Scripts46Script Integration47Script Logging48Error Handling49Script Performance50Parallel Processing51Remote Execution52Configuration Management53Script Monitoring54Automation Tools55Continuous Integration56Script Deployment57Script Security58Script Audit59Optimization Tips60Advanced Debugging
Tutorials/Linux & Bash/Bash Associative Arrays
🐧Linux & Bash

Bash Associative Arrays

Updated 2026-05-15
10 min read

Bash Associative Arrays

Introduction

Associative arrays are a powerful feature in Bash that allow you to store and manipulate data using key-value pairs. Unlike indexed arrays, which use numerical indices, associative arrays use strings as keys. This makes them particularly useful for tasks where you need to associate values with specific identifiers.

In this tutorial, we'll explore how to declare, initialize, access, modify, and delete elements in Bash associative arrays. We'll also look at some practical examples to help solidify your understanding.

Concept

Associative arrays were introduced in Bash version 4.0. They provide a way to map strings to values, which can be very handy for tasks such as configuration management, data processing, and more.

To declare an associative array, you use the declare command with the -A option:

Bash
1declare -A my_array

Once declared, you can assign values to keys in the array using the syntax array[key]=value. To access a value, you simply use array[key].

Examples

Declaring and Initializing an Associative Array

Let's start by declaring an associative array and initializing it with some key-value pairs:

Bash
1declare -A user_info
2user_info[name]="Alice"
3user_info[age]=30
4user_info[city]="New York"

Accessing Elements

To access the values in the associative array, you can use the keys:

Terminal
$ echo ${user_info[name]}
Output
Alice

You can also iterate over all elements in the array using a for loop:

Bash
1for key in "${!user_info[@]}"; do
2echo "$key: ${user_info[$key]}"
3done
Terminal
$ bash script.sh
Output
name: Alice
age: 30
city: New York

Modifying Elements

You can modify the value of an existing key by simply assigning a new value to it:

Bash
1user_info[age]=31
2echo ${user_info[age]}
Terminal
$ bash script.sh
Output
31

Adding New Elements

To add a new key-value pair, you can assign a value to a new key:

Bash
1user_info[country]="USA"
2echo ${user_info[country]}
Terminal
$ bash script.sh
Output
USA

Deleting Elements

To delete an element from the array, you can use the unset command:

Bash
1unset user_info[age]
2echo ${user_info[age]}
Terminal
$ bash script.sh
Output

Checking if a Key Exists

You can check if a key exists in the array using parameter expansion:

Bash
1if [[ -n "${user_info[name]}" ]]; then
2echo "Key 'name' exists."
3else
4echo "Key 'name' does not exist."
5fi
Terminal
$ bash script.sh
Output
Key 'name' exists.

What's Next?

In the next section, we'll dive into advanced functions in Bash, including how to create and use custom functions, handle arguments, and manage function scope. This will further enhance your scripting capabilities and allow you to write more complex and reusable code.

Stay tuned for more tutorials on Bash scripting!


PreviousBash ArraysNext Advanced Functions

Recommended Gear

Bash ArraysAdvanced Functions