commands:builtin:read

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
commands:builtin:read [2020/06/27 08:35]
thebonsai -e works with -u since 5.1 alpha
commands:builtin:read [2021/03/18 06:46] (current)
jarnos Modification by cfajohnson seems to contain false information. Changing IFS is not needed when no variable is used with read.
Line 15: Line 15:
  
 <WRAP center round info 90%> <WRAP center round info 90%>
-If no ''<​NAME>''​ is given, the whole line read (without performing word-splitting!) is assigned to the shell variable [[syntax:​shellvars#​REPLY|REPLY]]. ​The following code will strip leading and trailing whitespace from the input:+If no ''<​NAME>''​ is given, the whole line read (without performing word-splitting!) is assigned to the shell variable [[syntax:​shellvars#​REPLY|REPLY]]. ​Then, ''​REPLY''​ really contains ​the line as it was read, without stripping pre- and postfix spaces and other things!
 <​code>​ <​code>​
 while read -r; do while read -r; do
-  ​line=$REPLY +  ​printf '"​%s"​\n'​ "$REPLY" 
-  ... +done <<<" ​ a line with prefix and postfix space  "
-done < text.txt+
 </​code>​ </​code>​
 </​WRAP>​ </​WRAP>​
- 
-To preserve leading and trailing whitespace in the result, set IFS to the null string: 
- 
-<​code>​ 
-while IFS= read -r; do 
-  line=$REPLY 
-  ... 
-done < text.txt 
-</​code>​ 
- 
-Alternately,​ you can enclose $REPLY in double quotes and avoid fiddling with IFS altogether: 
- 
-<​code>​ 
-while read -r; do 
-  line="​$REPLY"​ 
-  ... 
-done < text.txt 
-</​code>​ 
  
 If a timeout is given, or if the shell variable [[syntax:​shellvars#​TMOUT|TMOUT]] is set, it is counted from initially waiting for input until the completion of input (i.e. until the complete line is read). That means the timeout can occur during input, too. If a timeout is given, or if the shell variable [[syntax:​shellvars#​TMOUT|TMOUT]] is set, it is counted from initially waiting for input until the completion of input (i.e. until the complete line is read). That means the timeout can occur during input, too.
  • commands/builtin/read.txt
  • Last modified: 2021/03/18 06:46
  • by jarnos