- Linux Shell Scripting Cookbook(Third Edition)
- Clif Flynt Sarath Lakshman Shantanu Tushar
- 117字
- 2021-07-09 19:46:10
Getting ready
Consider the case of CSV data:
data="name,gender,rollno,location" To read each of the item in a variable, we can use IFS. oldIFS=$IFS IFS=, # IFS is now a , for item in $data; do echo Item: $item done IFS=$oldIFS
This generates the following output:
Item: name Item: gender Item: rollno Item: location
The default value of IFS is a white-space (newline, tab, or a space character).
When IFS is set as , the shell interprets the comma as a delimiter character, therefore, the $item variable takes substrings separated by a comma as its value during the iteration.
If IFS is not set as , then it will print the entire data as a single string.