Hide error messages

4

Consider the following shell script:

ping  8.asd.8.8 -c1 -q > /dev/null
if [ $? == 0 ]
then
echo 'ok'
else
echo 'erro'
fi

This ping will return an error and this error is handled in else just below. But even using the -q parameter the error returned by the ping is written on the screen:

ping: unknown host 8.asd.8.8

Is there any way to ignore this error? Show only the error handled by if ?

    
asked by anonymous 16.09.2016 / 02:24

2 answers

3

Use &> , which in addition to redirecting standard output (stdout), redirects errors too (stderr):

ping  8.asd.8.8 -c1 -q &> /dev/null
    
16.09.2016 / 02:29
4

Redirect error output to /dev/null :

ping 8.asd.8.8 -c1 -q 2>/dev/null
if [ $? == 0 ]
then
echo 'ok'
else
echo 'erro'
fi

More information: What is and what is 2 > & 1 used for?

    
16.09.2016 / 02:31