'Output list of for loop as variable

I am trying to get the output of hostnames based on OS type (want only RedHat server hostnames) set as a variable.

but my code keeps spitting out the string RedHat along with each hostname.

minions=$(salt-run manage.up | cut -a " " -f2)

hosts=$(for minion in ${minions[@]}; do salt ${minion} grains.items | grep "os_family:" | grep RedHat && echo ${minion}; done)


Solution 1:[1]

By default grep will ouput the results of the pattern match.

If your version of grep supports it ... the -q flag will suppress the output:

... | grep -q RedHat && echo ${minion}; done)

Alternatively, redirect the output to /dev/null:

... | grep RedHat >/dev/null && echo ${minion}; done)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1