3

When I launch a beta command I've got this result :

dda6e95c-ecd1-4c22-a8df-f118af6aff02 Mount to DZ70cd on /dev/vda
f01e331e-0394-423f-b8ea-150f6b88029e Mount to DZ74cd on /dev/vdc
9f0d31ec-caa2-4e88-9cbd-4c82ebda5be3 Mount to DZe64c on /dev/vdc
2f3da174-1f9e-40e7-8869-563d90b103f4 Mount to DZ0d76 on /dev/vdc

I would like to apply a data-processing on each line received with a bash loop for as :

 for i in `beta-command` ; do echo $i  ; done ;

and I've got the resilt like this :

dda6e95c-ecd1-4c22-a8df-f118af6aff02
Mount
to
DZ70cd
on
/dev/vda
f01e331e-0394-423f-b8ea-150f6b88029e
Mount
to
DZ74cd
on
/dev/vdc

How to ask the loop to consider the line and not the term ?

Thank

dubis
  • 1,460

1 Answers1

4

You could set the Internal Field Separator (IFS) to newline:

IFS=$'\n'; for i in $(beta-command) ; do echo $i; done

Or use a while loop:

while IFS= read -r i; do
  echo $i
done < <(beta-command)

or use xargs:

beta-command | xargs -I{} echo {}

or use awk if your task is about manipulating text:

beta-command | awk '{print}'

Regarding the pros (?) and cons of using loops to process each line of command output or a file, you might read this (thanks @Jeff Schaller).

K7AAY
  • 3,816
  • 4
  • 23
  • 39
pLumo
  • 22,565