Skip to main content
deleted 4 characters in body
Source Link
Siva
  • 9.3k
  • 9
  • 60
  • 88

I am new to a bash script and writing the bash script. I have declared an array and iI have given the values. But When iI 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

I am new to 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

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

Source Link
clarie
  • 95
  • 1
  • 4
  • 12

Passing arrays in to the function in bash script

I am new to 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