User Tools

Site Tools


programming-languages:bash:loops

This is an old revision of the document!


Loops

For-loop

Two ways are available to perform a for-loop:

#!/bin/bash
 
echo "1st method"
for ((i=0; i<5; i=i+1)); do
    echo $i
done
 
echo "2nd method"
for i in {4..0}; do
    echo $i
done

While-loop

#!/bin/bash
 
i=0
while [[ i -lt 5 ]]; do
    echo $i
    i=$((i + 1))
done

Test on strings

expression is TRUE if
<str> <str> is not null
-n <str> <str> length is greater than zero
-z <str> <str> length is zero
<str1> == <str2> <str1> and <str2> are equal
<str1> != <str2> <str1> and <str2> are not equal
<str1> > <str2> <str1> comes after <str2> in lexicographically order
<str1> < <str2> <str1> comes before <str2> in lexicographically order

Test on integers

expression is TRUE if
<int1> -eq <int2> <int1> is equal to <int2>
<int1> -ne <int2> <int1> is not equal to <int2>
<int1> -le <int2> <int1> is less than or equal to <int2>
<int1> -lt <int2> <int1> is less than <int2>
<int1> -ge <int2> <int1> is greater than or equal to <int2>
<int1> -gt <int2> <int1> is greater than <int2>
programming-languages/bash/loops.1698834777.txt.gz · Last modified: 2023/11/01 11:32 by tormec