1

After reading up on how to initialize arrays in Bash, and seeing some basic examples put forward in blogs, there remains some uncertainties on its practical use. An interesting example perhaps would be to sort in ascending order -- list countries from A to Z in random order, one for each letter.

But in the real world, how is a Bash array applied? What is it applied to? What is the common use case for arrays? This is one area I am hoping to be familiar with. Any champions in the use of bash arrays? Please provide your example.

5
  • Um... any time you need a list of things. So, basically, all the time. Have you tried searching? They are used all over the place on this site and countless others. Commented Mar 30, 2016 at 20:57
  • It is useful whenever you need a list of strings and the individual strings might contain white-space. Do you want examples for when this would be needed? Commented Mar 30, 2016 at 20:59
  • Filenames, search terms, usernames, whatever. Every time you need to iterate over a list, i.e. to do the same operation to several things. Commented Mar 30, 2016 at 20:59
  • Associative arrays are really useful declare -A name. Before its introduction you can only resort to awk Commented Mar 30, 2016 at 21:14
  • @5gon12eder: ermm, I'm embarrassed to admit after the comments here so far. But in the spirit of learning, yes please. Commented Mar 30, 2016 at 21:14

1 Answer 1

6

There are a few cases where I like to use arrays in Bash.

  1. When I need to store a collections of strings that may contain spaces or $IFS characters.

    declare -a MYARRAY=(
        "This is a sentence."
        "I like turtles."
        "This is a test."
    )
    
    for item in "${MYARRAY[@]}"; do
        echo "$item" $(echo "$item" | wc -w) words.
    done
    
    This is a sentence. 4 words.
    I like turtles. 3 words.
    This is a test. 4 words.
    
  2. When I want to store key/value pairs, for example, short names mapped to long descriptions.

    declare -A NEWARRAY=(
         ["sentence"]="This is a sentence."
         ["turtles"]="I like turtles."
         ["test"]="This is a test."
    )
    
    echo ${NEWARRAY["turtles"]}
    echo ${NEWARRAY["test"]}
    
    I like turtles.
    This is a test.
    
  3. Even if we're just storing single "word" items or numbers, arrays make it easy to count and slice our data.

    # Count items in array.
    $ echo "${#MYARRAY[@]}"
    3
    
    # Show indexes of array.
    $ echo "${!MYARRAY[@]}"
    0 1 2
    
    # Show indexes/keys of associative array.
    $ echo "${!NEWARRAY[@]}"
    turtles test sentence
    
    # Show only the second through third elements in the array.
    $ echo "${MYARRAY[@]:1:2}"
    I like turtles. This is a test.
    

Read more about Bash arrays here. Note that only Bash 4.0+ supports every operation I've listed (associative arrays, for example), but the link shows which versions introduced what.

Sign up to request clarification or add additional context in comments.

3 Comments

If you don't like turtles, you cannot use single quotes.
Inside double quotes you can! Or am I missing something? Just fixed one quoting problem that I had, though.
It was just a silly joke. Your code is perfectly fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.