Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the "done" function/meaning in the bash while loop?

Tags:

linux

bash

I am a new guy for bash shell. I am confused by the below script.

#!/bin/bash

input=name.csv

while IFS=',' read -r Family_name First_name

do

 echo $Family_name
 echo $First_name

done < $input

Intuitively, I thought the done is some kind of boundary marker that told you the while field is over.

Here it shows that done can take data from a variable. so what is the meaning/function of done in the while loop? Thanks.

like image 908
Jiang Liang Avatar asked Feb 25 '26 15:02

Jiang Liang


2 Answers

Intuitively, I thought the done is some kind of boundary maker that told you the while field is over.

Correct; done ends a while-loop (or for-loop or similar).

Here it show that done can take the data from a variable.

No; the < $input (meaning < name.csv) is a redirection being applied to the whole while-loop. So it's the while-loop as a whole, not the done specifically, that takes input from name.csv.

like image 167
ruakh Avatar answered Feb 27 '26 04:02

ruakh


Intuitively, I thought the done is some kind of boundary maker that told you the while field is over.

That's true. done is certainly a part of the while loop itself. The syntax is:

   while list-1; do list-2; done

Here it show that done can take the data from a variable

The input redirection < is not specific to done keyword alone; rather it's for the entire loop and thus any input read from any statements from list-1 or list-2 would read from the file $input.

like image 29
P.P Avatar answered Feb 27 '26 03:02

P.P



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!