Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7895309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:36:42+00:00 2026-06-03T07:36:42+00:00

In bash when I access an array by index I get strange behavior if

  • 0

In bash when I access an array by index I get strange behavior if the array is a variable that was imported in the source of another bash script. What causes this behavior? How can it be fixed so an array that is sourced from another bash script behaves the same way as an array defined from within the running script?

${numbers[0]} evals to “one two three” and not “one” as it should.The full test I’ve tried to demonstrate this behavior is shown below:

Source of test.sh :

#!/bin/bash

function test {

    echo "Length of array:"
    echo ${#numbers[@]}

    echo "Directly accessing array by index:"
    echo ${numbers[0]}
    echo ${numbers[1]}
    echo ${numbers[2]}

    echo "Accessing array by for in loop:"
    for number in ${numbers[@]}
    do
        echo $number
    done

    echo "Accessing array by for loop with counter:"
    for (( i = 0 ; i < ${#numbers[@]} ; i=$i+1 ));
    do
        echo $i
        echo ${numbers[${i}]}
    done
}

numbers=(one two three)
echo "Start test with array from within file:"
test 

source numbers.sh
numbers=${sourced_numbers[@]}
echo -e "\nStart test with array from source file:"
test

Source of number.sh :

#!/bin/bash
#Numbers

sourced_numbers=(one two three)

Output of test.sh :

Start test with array from within file:
Length of array:
3
Directly accessing array by index:
one
two
three
Accessing array by for in loop:
one
two
three
Accessing array by for loop with counter:
0
one
1
two
2
three

Start test with array from source file:
Length of array:
3
Directly accessing array by index:
one two three
two
three
Accessing array by for in loop:
one
two
three
two
three
Accessing array by for loop with counter:
0
one two three
1
two
2
three
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:36:44+00:00Added an answer on June 3, 2026 at 7:36 am

    The problem has nothing to do with sourcing; it’s happening because the assignment numbers=${sourced_numbers[@]} doesn’t do what you think it does. It converts the array (sourced_numbers) into a simple string, and stores that in the first element of numbers (leaving “two” “three” in the next two elements). To copy it as an array, use numbers=("${sourced_numbers[@]}") instead.

    BTW, for number in ${numbers[@]} is the wrong way to loop through an array’s elements, because it’ll break on whitespace in the elements (in this case, the array contains “one two three” “two” “three”, but the loop runs for “one”, “two”, “three”, “two”, “three”). Use for number in "${numbers[@]}" instead. Actually, it’s good to get in the habit of double-quoting pretty much all variable substitutions (e.g. echo "${numbers[${i}]}"), as this is not the only place where leaving them unquoted could cause trouble.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a little Bash script that I use to access twitter and pop
Linux bash script: function Print() { echo $1 } Print OK This script runs
I previously used to copy Python/Perl scripts to access from my bash script. Duplication
I have a PHP script that calls a bash screen using exec and passes
I have a shell script that will let me access global variables inside the
Within a bash script, what would be the simplest way to verify that a
I wrote a short bash script to complete a task that involves creating a
I have a Unix shell script (bash) that sources some environment variables and then
How do I access eleent of $* array (or $@) by index ? For
I'm trying to write a bash script that sets up my web development environment

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.