Bash - Read (Builtin Command) that capture a line

Bash Liste Des Attaques Ovh

About

Read is a bash builtin command and read:

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

from:

and:

You could read a csv

Example

Prompting a user

Without message

  • The command
read myVar
  • You type hello
hello

  • You get it in the var myVar
echo $myVar
hello

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

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;
  • from a file. Example: All lines in an array (where \c0 replicate the eof character thanks to backslash escape)
IFS=$'\n' read -r -a REPO_DIRS -d $'\c0' < /path/to/myfile.csv

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 ?

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
done
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 whileDemo.sh

Syntax

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

where:

  • -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 variable name, the second word to the second name, and so on, If there are :

  • more words than names, leftover words, and their intervening separators are 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.





Discover More
Bash Liste Des Attaques Ovh
Bash - (Builtin|Intern|System|Reserved|Shell) variable name

Reserved variable name are named that have a special meaning for the bash shell. PS1 defines the shell's command-line prompt. HOME defines the home directory for a user. PATH defines a list...
Bash Liste Des Attaques Ovh
Bash - (Environment) Variable

A variable is a parameters referenced by a name. parameter A variable has: a value and zero or more attributes (such as integer, ...). Attributes are assigned using the declare builtin command....
Bash Liste Des Attaques Ovh
Bash - Builtin Commands

builtin refers to: a builtin command. See or to the specific builtin command. See (useful when defining a function whose name is the same as a shell builtin) The builtin command execute the specified...
Bash Liste Des Attaques Ovh
Bash - How to pass arguments that have space in their values ?

This article shows you how to pass arguments that have space characters in their values. Passing several arguments to a function that are stored as a string may not work properly when you have space...
Bash Liste Des Attaques Ovh
Bash - Standard input (stdin)

operations that applied only to the bash|bash shell See The input redirection operator takes the content of a file and send it as standard input Example with read that take a stand Create...
Bash Liste Des Attaques Ovh
Bash - Text Editing

By default, the line editing commands are similar to those of Emacs. A vi-style line editing interface is also available. Manipulating the text as you type it in. See: for word completion: for...
Bash Liste Des Attaques Ovh
Bash - While

This article is dedicated to the while syntax in Bash. where: : is the no-op command; its exit status is always 0 The command: lists the file with the ls executable (ls -l) pipe the...
Bash Liste Des Attaques Ovh
Bash - How to parse a CSV (Or properties files)

How to parse a CSV or property (ini) in bash where: the first FOR iterate over a list of ini file in the current directory. the while read: reads a line of ${FILE} until it finds an EOF parse...
Bash Liste Des Attaques Ovh
How to return two variables or more from a bash function?

This article shows you how you can return 2 or more variables from a bash function. Within your shell (remotely mostly within putty) Edit it with your favorite edition technique and add the bash...
Bash Liste Des Attaques Ovh
How to use the Array Variable in Bash?

Bash provides a one-dimensional array variables. See also: When calling a function, quote the variable otherwise bash will not see the string as atomic. The separator is variable $IFS. There is: ...



Share this page:
Follow us:
Task Runner