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 [2012/06/03 23:29]
cfajohnson [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>​
  
 ===== Description ===== ===== Description =====
 The ''​read''​ builtin reads **one line** of data (text, user input, ...) from standard input or a supplied filedescriptor number into one or more variables named by ''<​NAME...>''​. The ''​read''​ builtin reads **one line** of data (text, user input, ...) from standard input or a supplied filedescriptor number into one or more variables named by ''<​NAME...>''​.
 +
 +Since Bash 4.3-alpha, ''​read''​ skips any ''​NUL''​ (ASCII code 0) characters in input.
  
 If ''<​NAME...>''​ is given, the line is word-split using [[syntax:​shellvars#​IFS|IFS]] variable, and every word is assigned to one ''<​NAME>''​. The remaining words are all assigned to the last ''<​NAME>''​ if more words than variable names are present. If ''<​NAME...>''​ is given, the line is word-split using [[syntax:​shellvars#​IFS|IFS]] variable, and every word is assigned to one ''<​NAME>''​. The remaining words are all assigned to the last ''<​NAME>''​ if more words than variable names are present.
Line 30: Line 32:
 done < text.txt done < text.txt
 </​code>​ </​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.
 +
  
  
Line 38: 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 215: 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.1338766192.txt
  • Last modified: 2012/06/03 23:29
  • by cfajohnson