Bash - Read (Builtin Command) that capture a line
Table of Contents
1 - About
Read is a bash builtin command and read:
- by default one line
- or a number of characters (by option)
from:
- the standard input,
- or from the file descriptor fd supplied as an argument to the -u option
and:
2 - Articles Related
3 - Example
3.1 - Prompting a user
3.1.1 - Without message
- The command
read myVar
- You type hello
hello
- You get it in the var myVar
echo $myVar
hello
3.1.2 - With message
- The command
read -p "Type Something: " myVar
- You type hello
Type Something: hello
- You get it in the var myVar
echo $myVar
hello
3.2 - Read from a standard input
Example on how to read from a standard input
- from a pipeline (the block {} is important)
echo "hello world" | { read foo; echo foo=$foo; }
- from a string
read foo <<< "Hello World"; echo foo=$foo;
3.3 - Get the first two words (fields) of a line
Read parse the line with the IFS separators and assign the value to the names defined, we can then use it to parse a line.
Example
echo "hello world, how do you do ?" | { read foo bar mi; echo foo=$foo bar=$bar mi=$mi; }
foo=hello bar=world, mi=how do you do ?
- <note tip>tip: if you want to get rid of the command, add it as separator in IFS</note>
echo "hello world, how do you do ?" | { IFS=" ," read foo bar mi; echo foo=$foo bar=$bar mi=$mi; }
foo=hello bar=world mi=how do you do ?
4 - Syntax
read [-ers] [-u fd] [-t timeout] [-a aname] [-p prompt] [-n nchars] [-d delim] [name ...]
where:
- -r Backslash does not act as an escape character. The backslash is considered to be part of the line. In particular, a backslash-newline pair may not be used as a line continuation.
- -s Silent mode. If input is coming from a terminal, characters are not echoed.
- -u fd Read input from file descriptor fd.
- -t timeout Cause read to time out and return failure if a complete line of input is not read within timeout seconds. This option has no effect if read is not reading input from the terminal or a pipe.
- -a aname The words are assigned to sequential indices of the array variable aname, starting at 0. aname is unset before any new values are assigned. Other name arguments are ignored. If no names are supplied, the line read is assigned to the variable REPLY.
- -p prompt: Display prompt on standard error, without a trailing new- line, before attempting to read any input. The prompt is displayed only if input is coming from a terminal.
- -n nchars read returns after reading nchars characters rather than waiting for a complete line of input.
- -d delim: The first character of delim is used to terminate the input line, rather than newline.
- names the names that will receive the output. See below name assignment
4.1 - Name assignment
The first word is assigned to the first name, the second word to the second name, and so on, If there are :
- more words than names, leftover words and their intervening separators is assigned to the last name.
- fewer words than names, the remaining names are assigned empty values.
The characters in IFS are used to split the line into words. The backslash character (\) may be used to remove any special meaning for the next character read and for line continuation.
5 - Return code
The return code is zero, unless:
- end-of-file is encountered,
- read times out,
- or an invalid file descriptor is supplied as the argument to -u.