I have two arrays and want to print a value from ARRAY2 depending on the applicable value in ARRAY1.

#!/usr/bin/env bash

ARRAY1=(bb.service.sql bw.service.sql)
ARRAY2=(bb bw)

case $ARRAY1[@] in

    echo ${ARRAY2[1]} ;;

    echo "unknown" ;;

I m getting unknown here though. What am I doing wrong?

  • 1
    I'd suggest you start by pasting your code into shellcheck.net and fixing the errors Jan 18, 2023 at 12:32
  • In terms of debugging, echo case $ARRAY1[@] in just above your existing case statement will be helpful Jan 18, 2023 at 12:33

1 Answer 1


$ARRAY1 is short for ${ARRAY1[0]} in bash (a weird design copied from ksh).

$ARRAY1[@] there is the same as ${ARRAY[0]}[@], so the value of the element of index 0 followed by a literal [@]. While in many languages and many shells (starting with csh, the first shell to have had arrays) you can index arrays with $array[1], in bash, like in ksh, you need ${array[1]} instead. It was done that way so as not to break backward compatibility with the Bourne shell that did not have arrays.

${array[@]} would expand to all the elements of the array, but here since case it only to match strings, the elements are concatenated with spaces and that's what case will match against.

Also note that when you assign an array with array=(foo bar), foo goes in ${array[0]}, not 1 (same as in ksh, but unlike most other shells, including zsh where that array=(values...) syntax comes from.

In any case, it's not clear to me what you want to do with that.

Maybe you want to map bb to bb.service.sql instead, in which case, you'd rather use an associative array:

#! /bin/bash -
typeset -A map=(

(the ?s to error out, if the variables are not set).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .