Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save command output to variable without it printing to standard output

Tags:

linux

bash

I'm doing a bash script and I'm grabbing the output from this command:

fpings=$(fping -c 1 -t 1 $ips | sort) 

The fpings variable does contain the output from the command, and the actual output of the command is not printed to the shell, but it still writes a line to the shell for each ip pinged.

There is a switch (-q) to suppress the output (the part that I want) but not any to suppress the part I don't want.

How do I get the result from the fpings command without it printing stuff to the shell?

like image 784
Filip Haglund Avatar asked Jul 11 '13 21:07

Filip Haglund


2 Answers

If you do not want to see the standard error, redirect it to /dev/null:

fpings=$(fping -c 1 -t 1 $ips 2>/dev/null | sort) 
like image 158
choroba Avatar answered Oct 14 '22 17:10

choroba


fpings=$( {fping -c 1 -t 1 $ips | sort; } 2>&1 )

should work the {} capture everything and then it redirects both streams (out and err) to just out and saves in the variable

like image 38
exussum Avatar answered Oct 14 '22 18:10

exussum