1.5 KiB
		
	
	
	
	
	
	
	
			
		
		
	
	| categories | tags | ||
|---|---|---|---|
  | 
  | 
read
The primary use of read is to capture user input from stdin. It is also often used frequently 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.
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.
Example of capturing user input
$ read var1 var2
$ thomas bishop # user inputs this
$ echo $var2
$ bishop
If you don't specify variables,
readwill automatically parse using whitespace
Example of piping to read
This reads the files in a directory and passes the file names to read.
find -type -f  -not -path "./.git/" | read $fname
Example of parsing a file
We will typically read from a source and then do something with each variable that read returns, e.g:
while read var; do
  if [var == 'something']; then
    # do something
done < './input-file.txt
$REPLY
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:
while read;
do
    ((count++))
    if [[ -z "$REPLY" ]]; then
        echo "$count"
    fi
done < "$input