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

2
  • Good reading: unix.stackexchange.com/q/169716/117549 Commented Aug 22, 2018 at 14:30
  • HI Jeff good acknowledge of the post as the loop while concerned that didn 't appear in my own search Commented Aug 23, 2018 at 7:43

1 Answer 1

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).

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.