The problem of character Reading of read Command in linux

when using read, the following problems are encountered, which are not consistent with what you think. The code and results are as follows

-sharp!/bin/bash
IFS=" " read -d "-" var1 var2 <<< " 123 -"
printf "%s\n" "var1=$var1= var2=$var2="

IFS="|" read -d "-" bar1 bar2 <<< "|123|-"
printf "%s\n" "bar1=$bar1= bar2=$bar2="

my expected result should be

var1== var2=123=
bar1== bar2=123=

but the actual result is this

var1=123= var2==
bar1== bar2=123=

my question, for the first test example, since I specified the delimiter of IFS as""space, why isn"t var1 empty?

May.24,2021
The

read command is inherently delimited by spaces, and the effect is the same when you specify and unspecify IFS=''. And read is born to cut off the spaces at the beginning and end of a line, and this feature is unchangeable. The only way not to let read cut off the opening and ending spaces is to specify a blank IFS , such as IFS='' , but the space in the middle of the string is no longer treated as a delimiter. So the only way to do this is to replace the spaces in the string with other non-white space characters, such as commas, and so on, and then split them with IFS .

Menu