Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unix read command with option -d and IFS variable combination

Tags:

shell

unix

Looking forward to understand the behavior of read -d when it comes along with IFS variable

$ cat f1
a:b:c
d:e:f

$ while IFS= read -d: ; do echo $REPLY; done < f1
a
b
c d
e

$ while IFS=: read; do echo $REPLY; done < f1
a:b:c
d:e:f
like image 377
Ibrahim Quraish Avatar asked Dec 05 '25 16:12

Ibrahim Quraish


1 Answers

IFS is used when you're reading several variables with read:

$ echo foo:bar:baz | (IFS=: read FOO BAR BAZ; echo $FOO; echo $BAR; echo $BAZ)
foo
bar
baz

Whereas, the -d option specifies what your line separator for read is; read won't read beyond a single line:

$ echo foo:bar:baz%baz:qux:quux% | while IFS=: read -d% FOO BAR BAZ; do echo ---; echo $FOO; echo $BAR; echo $BAZ; done
---
foo
bar
baz
---
baz
qux
quux
like image 107
Chris Jester-Young Avatar answered Dec 07 '25 15:12

Chris Jester-Young