Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
commands:builtin:read [2013/04/14 11:14]
thebonsai [Description]
commands:builtin:read [2018/07/17 08:42] (current)
thebonsai [Synopsis]
Line 4: Line 4:
 ===== Synopsis ===== ===== Synopsis =====
 <​code>​ <​code>​
-read [-ers] [-u <FD>] [-t <​TIMEOUT>​] [-p <​PROMPT>​] [-a <​ARRAY>​] [-n <​NCHARS>​] [-d <​DELIM>​] [-i <​TEXT>​] [<​NAME...>​]+read [-ers] [-u <FD>] [-t <​TIMEOUT>​] [-p <​PROMPT>​] [-a <​ARRAY>​] [-n <​NCHARS>​] [-N <​NCHARS>​] [-d <​DELIM>​] [-i <​TEXT>​] [<​NAME...>​]
 </​code>​ </​code>​
  
Line 29: Line 29:
 while IFS= read -r; do while IFS= read -r; do
   line=$REPLY   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 done < text.txt
Line 43: Line 52:
 |''​-d <​DELIM>''​|recognize ''<​DELIM>''​ as data-end, rather than ''<​newline>''​| |''​-d <​DELIM>''​|recognize ''<​DELIM>''​ as data-end, rather than ''<​newline>''​|
 |''​-e''​|on interactive shells: use Bash's readline interface to read the data| |''​-e''​|on interactive shells: use Bash's readline interface to read the data|
-|''​-i <​STRING>''​|:V4: preloads the input buffer with text from ''<​STRING>'',​ only works when Readline (''​-e''​) is used|+|''​-i <​STRING>''​|preloads the input buffer with text from ''<​STRING>'',​ only works when Readline (''​-e''​) is used|
 |''​-n <​NCHARS>''​|reads ''<​NCHARS>''​ characters of input, then quits| |''​-n <​NCHARS>''​|reads ''<​NCHARS>''​ characters of input, then quits|
-|''​-N <​NCHARS>''​|:V4: reads ''<​NCHARS>''​ characters of input, //ignoring any delimiter//,​ then quits|+|''​-N <​NCHARS>''​|reads ''<​NCHARS>''​ characters of input, //ignoring any delimiter//,​ then quits|
 |''​-p <​PROMPT>''​|the prompt string ''<​PROMPT>''​ is output (without a trailing automatic newline) before the read is performed| |''​-p <​PROMPT>''​|the prompt string ''<​PROMPT>''​ is output (without a trailing automatic newline) before the read is performed|
 |''​-r''​|raw input - **disables** interpretion of **backslash escapes** and **line-continuation** in the read data| |''​-r''​|raw input - **disables** interpretion of **backslash escapes** and **line-continuation** in the read data|
 |''​-s''​|secure input - don't echo input if on a terminal (passwords!)| |''​-s''​|secure input - don't echo input if on a terminal (passwords!)|
-|''​-t <​TIMEOUT>''​|wait for data ''<​TIMEOUT>''​ seconds, then quit (exit code 1). :V4: Fractional seconds ("​5.33"​) are allowed. A value of 0 immediately returns and indicates if data is waiting in the exit code. Timeout is indicated by an exit code greater than 128. If timeout arrives before data is read completely (before end-of-line),​ the partial data is saved.|+|''​-t <​TIMEOUT>''​|wait for data ''<​TIMEOUT>''​ seconds, then quit (exit code 1). Fractional seconds ("​5.33"​) are allowed ​since Bash 4. A value of 0 immediately returns and indicates if data is waiting in the exit code. Timeout is indicated by an exit code greater than 128. If timeout arrives before data is read completely (before end-of-line),​ the partial data is saved.|
 |''​-u <​FD>''​|use the filedescriptor number ''<​FD>''​ rather than ''​stdin''​ (0)| |''​-u <​FD>''​|use the filedescriptor number ''<​FD>''​ rather than ''​stdin''​ (0)|
  
Line 220: Line 229:
 ==== Ask for a path with a default value ==== ==== Ask for a path with a default value ====
  
-__**Note:​**__ ​:V4: The ''​-i''​ option was introduced with Bash 4+__**Note:​**__ The ''​-i''​ option was introduced with Bash 4
  
 <​code>​ <​code>​
  • commands/builtin/read.1365938059.txt
  • Last modified: 2013/04/14 11:14
  • by thebonsai