This is an old revision of the document!
BASH - Files - Read a file - Read fields from a file
To read fields within each line of the file, additional variables may be used with the read:
For instance, if an input file has 3 columns separated by white space (space or tab characters only).
while read -r first_name last_name phone; do # Only print the last name (second column). printf '%s\n' "$last_name" done < "$file"
If the field delimiters are not whitespace, set the IFS (internal field separator):
# Extract the username and its shell from /etc/passwd: while IFS=: read -r user pass uid gid gecos home shell; do printf '%s: %s\n' "$user" "$shell" done < /etc/passwd
NOTE: IFS is set to :.
For tab-delimited files, use IFS=$'\t' though beware that multiple tab characters in the input will be considered as one delimiter (and the IFS=$'\t\t' workaround will not work in Bash).
You do not necessarily need to know how many fields each line of input contains.
- If you supply more variables than there are fields, the extra variables will be empty.
- If you supply fewer, the last variable gets “all the rest” of the fields after the preceding ones are satisfied.
For example:
read -r first last junk <<< 'Bob Smith 123 Main Street Elk Grove Iowa 123-555-6789'
- first: will contain “Bob”
- last: will contain “Smith”.
- junk: holds everything else.
The throwaway variable _ can be used as a “junk variable” to ignore fields.
- It (or indeed any variable) can also be used more than once in a single read command, if we don't care what goes into it:
read -r _ _ first middle last _ <<< "$record"
- We skip the first two fields, then read the next three.
- The final _ can absorb any number of fields.
- It does not need to be repeated there.
This usage of _ is only guaranteed to work in Bash.
- Many other shells use _ for other purposes that will at best cause this to not have the desired effect, and can break the script entirely.
- It is better to choose a unique variable that isn't used elsewhere in the script, even though _ is a common Bash convention.