Reputation: 99
The variable $type
is a multi line string variable containing:
car
plane
You can get the output above issuing echo "$type"
.
And I have a some_command
that uses $type
as one of its parameters.
In bash, how would you execute some_command
so that it runs with each line of $type
one after another? i.e. first some_command
would run using car
and then it would run again using plane
and then the loop would stop once the line was empty i.e. no values left to execute some_command
with.
Upvotes: 1
Views: 2737
Reputation: 72226
The easiest way (in my opinion) is to use xargs
with argument -L
(limit):
echo "$type" | xargs -L 1 some_command
xargs
takes each input line and passed the value as an argument to some_command
.
some_command
may have other arguments too. xargs
adds the input line at the end of its arguments list and executes it.
On macOS it can be told to insert the input line into a different position by using -J
but this is a non-standard FreeBSD extension that is not available in the GNU version of xargs
(the version usually present on the Linux systems.)
Upvotes: 1
Reputation: 6061
Supposing the lines in $type
do not contain white spaces other than the new line chars, try:
for line in $type; do echo working on "$line"; some_command "$line"; done
It is important not to surround the first occurence of $type
with double quotes in the for statement, otherwise you will get all the lines at once.
Upvotes: 0