Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Tried all sorts of ways to redirect both stdout and stderr to /dev/null without any success. I have almost my entire life run bash which i've never had this issue with but for once in BSD i'm stuck with /bin/sh.

What i've tried:

if ls ./python* 2> /dev/null; then
    echo found Python
fi

Which works, if Python is not present it will mute the error messages from ls. However, if python.tgz is present a line with be outputted which looks like this:

# ./test.sh
./python-2.7.3p1.tgz

And i've tried:

if ls ./python* &> /dev/null; then
    echo found Python
fi

and

if ls ./python* 2>1 > /dev/null; then
    echo found Python
fi

and

if ls ./python* > /dev/null; then
    echo found Python
fi

Nothing really works. I can only redirect one of the outputs, not both at the same time.

share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

I believe what you are looking for is:

ls good bad >/dev/null 2>&1

You have to redirect stdout first before duplicating it into stderr; if you duplicate it first, stderr will just point to what stdout originally pointed at.

In bash you can do this with &>/dev/null but that's a bash extension.

share|improve this answer
    
Indeed, i read the bourn shell manual. It stated that later versions of /bin/sh have implemented the &>/dev/null syntax, aparently not so or i have a older version (which i can't echo in any way, running OpenBSD 5.3 tho so should be sufficient) –  Torxed Jun 25 '13 at 19:29
1  
@Torxed, OpenBSD's sh is based on pdksh. There's no more Bourne shell nowadays. csh introduced >& also available in zsh. bash chose &> (now also supported by zsh and some pdksh derivatives) though it clearly breaks POSIX compliance since foo &> file is perfectly valid POSIX syntax which means something completely different. –  Stephane Chazelas Jun 25 '13 at 19:42
    
Thank you for clearing that out! –  Torxed Jun 25 '13 at 20:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.