Bash - Read (Builtin Command) that capture a line


Read is a bash builtin command and read:

  • by default one line
  • or a number of characters (by option)



You could read a csv


Prompting a user

Without message

  • The command
read myVar
  • You type hello
  • You get it in the var myVar
echo $myVar

With message

  • The command
read -p "Type Something: " myVar
  • You type hello
Type Something: hello
  • You get it in the var myVar
echo $myVar

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;

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.


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 ?

Loop line by line

This command:

  • lists the file with the ls executable (ls -l)
  • pipe the output to the condition of thewhile statement
  • where the read condition splits the input by line and stores the line content in the lineVariable variable
ls -l | while read -r lineVariable; do
    echo Line: $lineVariable
Line: -rw-r--r-- 1 root root 1849173 May 13 2015 unixODBC-2.3.2.tar.gz
Line: -rwxr-xr-- 1 oracle oinstall 64 May 16 2017


read [-ers] [-u fd] [-t timeout] [-a aname] [-p prompt] [-n nchars] [-d delim] [name ...]


  • -e If the standard input is coming from a terminal, readline is used to obtain the line.
  • -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

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.

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.

Powered by ComboStrap