The read Command and Here-Strings
In this page:
Reading a Value Without Real stdin
The read builtin reads a line from its input and stores it in one or more variables. A here-string (<<<) lets you hand it a literal string as if it had been typed, which is perfect for scripts that have no interactive terminal attached.
Note: Always prefer -r unless you specifically want backslash escapes interpreted.
Example: Reading a Value Without Real stdin
#!/bin/bash
read -r name <<< "Ada Lovelace"
echo "Hello, $name"
Login to try C/C++/Java/PHP code in the editor
Reading Multiple Variables at Once
When read is given several variable names, it splits the input on IFS (whitespace by default) and assigns one field per variable. Any leftover words are all stuffed into the last variable.
Example: Reading Multiple Variables at Once
#!/bin/bash
read -r first last <<< "Grace Hopper"
echo "First: $first"
echo "Last: $last"
read -r a b c <<< "one two three four"
echo "c holds the rest: $c"
Login to try C/C++/Java/PHP code in the editor
Reading Into an Array
The -a flag tells read to split the input on IFS and store every field into an indexed array in one shot, which is often cleaner than declaring several named variables.
Example: Reading Into an Array
#!/bin/bash
read -r -a words <<< "bash scripting is fun"
echo "Word count: ${#words[@]}"
echo "Second word: ${words[1]}"
Login to try C/C++/Java/PHP code in the editor
Custom Field Separator with IFS
You can temporarily change IFS just for one read call to split on something other than whitespace, such as a colon, which is handy for parsing lines like /etc/passwd entries.
Note: Setting IFS= right before the command only affects that single command, so it does not leak into the rest of the script.
Example: Custom Field Separator with IFS
#!/bin/bash
IFS=':' read -r user pass uid <<< "root:x:0"
echo "User: $user, UID: $uid"
Login to try C/C++/Java/PHP code in the editor
- Assuming
readalways waits for a human to type something; it happily reads from any input source, including a here-string or a file. - Forgetting that
readwithout-rwill interpret backslashes as escape characters, silently mangling paths likeC:\temp. - Not realizing a here-string automatically appends a trailing newline to the text before feeding it to the command.
read var <<< "text"feedstexttoreadas if it were typed, without needing real stdin.read -rdisables backslash escaping and should be your default.read a b c <<< "x y z"splits on whitespace (IFS) into multiple variables at once.read -a arr <<< "..."reads whitespace-separated words directly into an array.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first:
- Function Basics
- The read Command and Here-Strings
- Function Arguments
- stdin, stdout, stderr and File Descriptors
- Return Values
- Redirection Operators
- Local Variables
- Pipes and Chaining Commands
- Recursive Functions
- Here-Documents (<<EOF)
- Function Libraries
- The tee Command
- Discarding Output with /dev/null