1

I am new to a bash script and writing the bash script. I have declared an array and I have given the values. But When I pass the array in the loop values are not getting inside the loop. Below is my code

declare -a files=( "A1S0" "D1S0" "D2S0" "D3S0" "D4S0" "D5S0" "D6S0" ) command_to_get_filesizes() { for i in "${!files[@]}"; do echo Processing file "${i[${index}]}" done } command_to_get_filesizes 

Expected Output

A1S0 D1s0 D2S0 D3S0 D5S0 D680 

But am getting output as

Processing file 0 Processing file 1 Processing file 2 Processing file 3 Processing file 4 Processing file 5 Processing file 6 

could anyone pls help me to print values in the array

    2 Answers 2

    0

    The variable 'index' is undefined. What you intended was

    echo Processing file "${files[i]}" 

    What the original does is obscure. All variables are (potentially) arrays with a zeroth element. (Try echo "${PATH[$junk]}".) So that whole construct "${i[${index}]}" resolves as ${i[0]}, which just echoes $i.

    Your title is slightly misleading: you do not pass the array into the function (as a parameter): it is merely a global array variable.

    @Siva is correct in that you can iterate over the array values simply. However, if you need both the index and the value in the same iteration, this does it for me.

    for n in "${!N[@]}"; do printf '%8d %s\n' "${n}" "${N[n]}" done 
    4
    • What do you mean by "${1[${i}]}" works? I get a bad substitution error in bash 5.0.16CommentedMay 22, 2020 at 11:04
    • Note that you can also do ${files[i]} which saves the double translation between integer and number.CommentedMay 22, 2020 at 11:05
    • Yes, the index part of an indexed array element notation is an arithmetic expression, and the $ is not required for shell variables in an arithmetic expression: "${N[n]}"CommentedMay 22, 2020 at 12:01
    • @StéphaneChazelas Had I not tested this, I would not have had the opportunity to be astonished. But it has vanished from my history, and I can't reconstruct what I thought I did. So conceded and edited. I did run across this construct which lets you alter global arrays by variable name from within functions: declare -g "$1[4]=abc";CommentedMay 23, 2020 at 9:38
    0

    As per your expected output,

    • we don't require "Processing file" in that echo command.
    • we don't need to pass the index value of array while looping it in for loop.

    Try as

    declare -a files=( "A1S0" "D1S0" "D2S0" "D3S0" "D4S0" "D5S0" "D6S0" ) command_to_get_filesizes() { for i in "${files[@]}"; do echo "$i" done } command_to_get_filesizes 
    1
    • The ! is not the "not" operator in this context. It makes the expression expand into the indexes of the array, instead of the values. Necessary for associative arrays, but also useful for numeric indices (which can be sparse but are still processed in numeric order).CommentedMay 23, 2020 at 9:33

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.