I have 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
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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
This will work in any Posix-compatible shell:
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.
Bash, zsh and some other shells also provide the shortcut
ls good bad &>/dev/null
which is convenient on the command-line but should be avoided in scripts which are intended to be portable.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0