I have a bash script with following variable:
operators_list=$'andrii,bogdan,eios,fre,kuz,pvm,sebastian,tester,tester2,vincent,ykosogon'
while IFS=, read -r tech_login; do
echo "... $tech_login ..."
done <<< "$operators_list"
I need to read arguments from variable and work with them in loop. But it returns echo only one time with all items:
+ IFS=,
+ read -r tech_login
+ echo '... andrii,bogdan,eios,fre,kuz,pvm,sebastian,tester,tester2,vincent,ykosogon ...'
... andrii,bogdan,eios,fre,kuz,pvm,sebastian,tester,tester2,vincent,ykosogon ...
+ IFS=,
+ read -r tech_login
What am I doing wrong? How to rework script, so it will work only with one item per time?
operators_list=$'andrii,bogdan,eios,fre,kuz,pvm,sebastian,tester,tester2,vincent,ykosogon'
So you have strings separated by ,
. You can do that multiple ways:
IFS=, read -a operators <<<$operators_list
for op in "${operators[@]}"; do
echo "$op"
done
while IFS= read -d, -r op; do
echo "$op"
done <<<$operators_list
<<<$operators_list xargs -d, -n1 echo
The thing with IFS and read delimeter is: read
reads until delimeter specified with -d
. Then after read
has read a full string (usually whole line, as default delimeter is newline), then the string is splitted into parts using IFS
as delimeter. So you can:
while IFS=: read -d, -r op1 op2; do
echo "$op1" "$op2"
done <<<"op11:op12,op12:op22"