Syntax of for Loop in Linux Shell Scripting

Shell script for loop syntax

Brace expansion, {x..y} is performed before other expansions, so you cannot use that for variable length sequences.

Instead, use the seq 2 $max method as user mob stated.

So, for your example it would be:

max=10
for i in `seq 2 $max`
do
echo "$i"
done

syntax of for loop in linux shell scripting

You probably run it with sh, not bash. Try bash test1.sh, or ./test1.sh if it's executable, but not sh test1.sh.

How do I write a 'for' loop in Bash?

From this site:

for i in $(seq 1 10);
do
echo $i
done

Nested for loop in unix shell script

You can't loop over an index+value pair directly in Bash. I would write this code as follows:

index=0
for path in /optware/oracle/logs/20190311_JAVA/TEMP/*
do
echo " <tr>${index}<td></td><td>${path}</td></tr>" >> Temp.lst
((++index))
done

How do I iterate over a range of numbers defined by variables in Bash?

for i in $(seq 1 $END); do echo $i; done

edit: I prefer seq over the other methods because I can actually remember it ;)



Related Topics



Leave a reply



Submit