Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting for Loop from second element - Shell script

Tags:

linux

shell

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.

like image 580
Saurabh Avatar asked May 12 '14 09:05

Saurabh


People also ask

How do I run a loop in bash?

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!"

What does $() mean in shell script?

$() – the command substitution. ${} – the parameter substitution/variable expansion.

How do you write a loop in shell script?

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.

What does $() mean in bash?

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).


1 Answers

You can use ${number[@]:1} to start iterating from 2nd element:

for i in "${number[@]:1}"; do
    echo "Processing: $i"
done
like image 100
anubhava Avatar answered Nov 10 '22 02:11

anubhava