I want to start iterating over the array from the second element for the below array in shell script.
number=${number:-(12 20 43 45 67 40)}
Could you please help me on how to use the For Loop to iterate starting from the second element (ie 20 in this case)
for i in ${number[@]}
Thanks in advance.
To demonstrate, add the following code to a Bash script: #!/bin/bash # Infinite for loop with break i=0 for (( ; ; )) do echo "Iteration: ${i}" (( i++ )) if [[ i -gt 10 ]] then break; fi done echo "Done!"
$() – the command substitution. ${} – the parameter substitution/variable expansion.
1) Syntax:Syntax of for loop using in and list of values is shown below. This for loop contains a number of variables in the list and will execute for each item in the list. For example, if there are 10 variables in the list, then loop will execute ten times and value will be stored in varname.
Example of command substitution using $() in Linux: Again, $() is a command substitution which means that it “reassigns the output of a command or even multiple commands; it literally plugs the command output into another context” (Source).
You can use ${number[@]:1}
to start iterating from 2nd element:
for i in "${number[@]:1}"; do
echo "Processing: $i"
done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With