standard error to standard out question

Hi there

how can i get the result of a command to not give me its error. For example, on certain systems the 'zfs' command below is not available, but this is fine becaues I am testing against $? so i dont want to see the message " command not found" Ive tried outputting to /dev/null 2>&1 to no avail...see below ..

Does anybody knwo how i can stop this output to standard out ?

# zfs list | grep fred > /dev/null 2>&1
bash: zfs: command not found
#

You're setting up redirection for that command, while the message is sent by the shell. You can either redirect that too by using exec, or you can do it the smart way:

if [ -x /path/to/zfs ]
then
    zfs list | grep ...
    ...
else
    # Whatever to be done if zfs isn't there
fi

By the way, grep knows the -q switch, which only sets $? based on whether the term is found or not, and produces no additional output.

Ok thanks, if I were to put that 'if' statement in there it would mean a bit of a change to the script so im particularly interested in the 'exec' method you mention.... ive tried

# exec zfs list | grep $HOME$ > /dev/null 2>&1
bash: exec: zfs: not found

but as you can see i still get the message, is this what you mean ??

Even if it would mean changing some of your script, I don't think that it would be too much in relation to gained stability and maintainability, eg if your previous code was

zfs list | grep fred >/dev/null 2>&1
if [ $? -ne 0 ]...

a change would be as simple as

if [ -x /path/to/zfs ]
then
    zfs list | grep fred >/dev/null 2>&1
    zfs_found=$?
else
    zfs_found=1
fi
if [ $zfs_found -ne 0 ]...

If, however, you are absolutely sure that there is no other way, at the top of your script put the line

exec 2>/dev/null

which will redirect all stderr messages to /dev/null (even those that might indicate a serious problem)

zfs list 2> /dev/null | grep fred

Thats fantastic ...works perfectly..thanks cambridge

and thanks pludi for you help as well