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

  • Home
  • SEARCH
  • 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 8689315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:36:56+00:00 2026-06-12T23:36:56+00:00

I am new to shell scripting and am trying to get all the android

  • 0

I am new to shell scripting and am trying to get all the android devices in an array but my array is empty when the function is finished.

#!/bin/bash

declare -a arr
let i=0

MyMethod(){
  adb devices | while read line #get devices list
  do
    if [ ! "$line" == "" ] && [ `echo $line | awk '{print $2}'` == "device" ]
    then
      device=`echo $line | awk '{print $1}'`
      echo "Add $device"
      arr[$i]="$device"
      let i=$i+1
    fi
  done

echo "In MyMethod: ${arr[*]}"
}

################# The main loop of the function call #################

MyMethod
echo "Not in themethod: ${arr[*]}"

arr – is empty, what am I doing wrong?

Thanks for advice.

  • 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-12T23:36:58+00:00Added an answer on June 12, 2026 at 11:36 pm

    Your probable problem is that piping a command causes it to run in a sub-shell, and variables that are changed there aren’t propagated to the parent shell. You’re solution would probably be something like:

    adb devices > devices.txt
    while read line; do
        [...]
    done < devices.txt
    

    where we save the output into an intermediary file to then be loaded into the while loop, or maybe use bash’s syntax to store command output into intermediary temporary files:

    while read line; do
        [...]
    done < <(adb devices)
    

    So the script becomes:

    #!/bin/bash
    
    declare -a arr
    let i=0
    
    MyMethod(){
      while read line #get devices list
      do
        if [ -n "$line" ] && [ "`echo $line | awk '{print $2}'`" == "device" ]
        then
          device="`echo $line | awk '{print $1}'`"
          echo "Add $device"
          arr[i]="$device" # $ is optional
          let i=$i+1
        fi
      done < <(adb devices)
    
    echo "In MyMethod: ${arr[*]}"
    }
    
    ################# The main loop of the function call #################
    
    MyMethod
    echo "Not in themethod: ${arr[*]}"
    

    Some extra observations:

    1. To avoid errors, I would suggest you surround the back quotes in double quotes.
    2. The dollar is optional in arr[$i]=
    3. There is a specific test for empty strings: [ -z "$str" ] checks if string is empty (zero-length) and [ -n "$str"] checks if it isn’t

    Hope this helps =)

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

Sidebar

Related Questions

I'm very new to shell scripting and I am trying to get to grips
I am new to bash-scripting & trying to understand how things work. It's all
I'm new to bash scripts (and the *nix shell altogether) but I'm trying to
I am new at shell scripting and I am trying to read data from
Forgive my ignorance, but I am somewhat new to shell scripting and the use
I'm new to bash shell scripting and it is my first day and first
I am new to shell scripting and i am trying to remove new line
First off I am very very new to shell scripting. I am trying to
I am new to shell scripting. I am trying to work through this. >
I'm really new to UNIX/Shell Scripting I'm trying to extract disk usage from numerous

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.