Sunday, January 9, 2022

[SOLVED] linux shell how many loop various

Issue

Linux shell has so many loops syntax and how to choice it!

while read -r line; do
   echo "$line"
done < input.file
for line in `cat a.txt`; do
  echo "$line"
while

and so on


Solution

you can make a test yourself, such as

write a file test.c

#include <stdio.h>
int main(void){
        printf("hello world\n");
        return 0;
}

and then using the two command

A: while read -r line;do echo $line; done < test.c

you will get this result

#include <stdio.h>
int main(void){
printf("hello world
");
return 0;
}

and B: for line in cat test.c;do echo $line;done

you get the result like this:

#include
<stdio.h>
int
main(void){
printf("hello
world
");
return
0;
}

and now you will find the differences.



Answered By - baozilaji