Skip to main content
InterviewHubPRO
Section Overview
Q17

Question 17 of 50

Part 2: Core Architecture & Implementation (Mid-Level)

34%
Bash & Shell Scripting (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)

How do you manipulate Arrays and Associative Arrays in Bash?

senior LevelCode ExampleSystem DesignData Structures

In Simple Words:

  • Indexed Array:
    bash
    servers=("app01" "app02" "db01")
    servers+=("cache01")         # Append
    echo "${servers[0]}"         # app01
    echo "${#servers[@]}"        # Length: 4
    for s in "${servers[@]}"; do echo "$s"; done
    
  • Associative Array (Key-Value Dictionary, Bash 4+):
    bash
    declare -A ports
    ports["http"]=80
    ports["https"]=443
    echo "${ports["https"]}"    # 443