2022-12-15 17:00:05 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								categories:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  -  Programming Languages
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								tags:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  -  shell
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-02-26 11:56:30 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								# read
 
							 
						 
					
						
							
								
									
										
										
										
											2022-12-15 17:00:05 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-03-08 07:11:40 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								The primary use of `read`  is to capture user input from `stdin` . It can also be used to parse strings or files that are redirected to it (with `<`  and `<<` ) or piped to it. In each case, what is read is stored as a variable.
							 
						 
					
						
							
								
									
										
										
										
											2022-12-15 17:00:05 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-02-07 08:44:16 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								`read`  will parse line by line using a space (`\n` ) as the default delimiter. You can use IFS to parse by other characters and/or [split the contents into an array ](/Programming_Languages/Shell/Split_into_array.md ).
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								## Example of capturing user input
 
							 
						 
					
						
							
								
									
										
										
										
											2022-12-15 17:00:05 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```bash
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								$ read var1 var2
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								$ thomas bishop # user inputs this
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								$ echo $var2
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								$ bishop
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								>  If you don't specify variables, `read` will automatically parse using whitespace
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-03-08 07:11:40 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								## Example of piping to read
 
							 
						 
					
						
							
								
									
										
										
										
											2023-02-07 08:44:16 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-03-08 07:11:40 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Here we use [find ](/Programming_Languages/Shell/Find.md ) to collate the files in the current directory and then pipe them to read.
							 
						 
					
						
							
								
									
										
										
										
											2022-12-15 17:00:05 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```bash
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								find -type -f  -not -path "./.git/" | read $fname
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2023-02-07 08:44:16 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								## Example of parsing a file
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								We will typically read from a source and then do something with each variable that `read`  returns, e.g:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```bash
							 
						 
					
						
							
								
									
										
										
										
											2023-03-08 07:11:40 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								while read line; do
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  if [ var == 'something' ]; then
							 
						 
					
						
							
								
									
										
										
										
											2023-02-07 08:44:16 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    # do something
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								done <  './input-file.txt
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-02-26 11:56:30 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								## $REPLY
 
							 
						 
					
						
							
								
									
										
										
										
											2023-02-07 08:44:16 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								If you do not assign a variable name to store the value that `read`  reads a default (`$REPLY` ) is applied. You can reference this value in your code.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								For example the following loop does something if `$REPLY`  is equal to an empty string:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```bash
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								while read;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								do
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    ((count++))
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    if [[ -z "$REPLY" ]]; then
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        echo "$count"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    fi
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								done <  "$input
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```