bash foreach loop equivalent in linux
CSH/TCSH
Foreach is csh built-in command for control flow,below is some examples
[j@localhost ~]$ foreach x (a b c 1 2 3) foreach? echo $x foreach? end a b c 1 2 3
OR iterate from file
[j@localhost ~]$ foreach x ( `cat file.txt ` ) foreach? echo "Here is the $x" foreach? end Here is the 1stline Here is the 2ndline Here is the 3rdline [j@localhost ~]$
BASH
In bash there is no foreach , so we can use “for” instead
[j@localhost ~]$ for x in a b c 1 2 3;do echo $x;done a b c 1 2 3
Or if need to iterate from a file
[j@localhost ~]$ for x in `cat file.txt`;do echo "Here is $x";done Here is 1stline Here is 2ndline Here is 3rdline
Or we can also use “while” to iterate from file
[j@localhost ~]$ while read file > do > echo "Here is $file" > done < file.txt Here is 1stline Here is 2ndline Here is 3rdline [j@localhost ~]$