Bash for loop

Below code will print 0 to 100 in bash shell

for ((i = 0 ; i < 100 ; i++)); do
  echo "$i"
done

Ranges for for loop

for i in {1..5}; do
    echo "Welcome to my blog $i"
done

Expected result
Welcome to my blog 1
Welcome to my blog 2
Welcome to my blog 3
Welcome to my blog 4
Welcome to my blog 5

With step size of 5

for i in {5..50..5}; do
    echo "Welcome to my blog  $i"
done

Expected result
Welcome to my blog  5
Welcome to my blog  10
Welcome to my blog  15
Welcome to my blog  20
Welcome to my blog  25
Welcome to my blog  30
Welcome to my blog  35
Welcome to my blog  40
Welcome to my blog  45
Welcome to my blog  50

Reading lines from a file (file created with few line)

lab01> while read -r line; do echo “$line”; done <file.txt

Expected output from file

apple
orange
toy
car
bus
auto

While loop executing every 30 sec with sleep command

lab01> while true; do ps -ef | grep 'ssh';sleep 30;done

Listing files in a directory

for file in *; do echo $file; done

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *