-1

Considering the following shellscript:

#!/bin/bash
echo test > /usr/lib/permDenied 2>&1 > /dev/null 
echo $?

This gives me the following error:

$ ./test.sh 
./test.sh: line 2: /usr/lib/permDenied: Permission denied
1

How can I make the error silent and get the error code as well?

morandg
  • 1,066
  • 3
  • 14
  • 30
  • Arfff I tried everything I had in mind but not `echo test 2>/dev/null > /usr/lib/permDenied` ... So frusttrating, I should have known :p! – morandg May 25 '16 at 16:30
  • This is a duplicate question, I'm sure-- but I'm not looking for it :-) – SaintHax May 25 '16 at 20:25
  • shell command line is order dependant! `2>&1 >file` is not equiv than `>file 2>&1` !! see http://stackoverflow.com/questions/818255/in-the-shell-what-does-21-mean/16283739#16283739 – F. Hauri - Give Up GitHub May 25 '16 at 20:34

1 Answers1

-1

A search would have found this, but here's an explanation as to why it wasn't working for the future.

echo test > /usr/lib/permDenied 2>&1 > /dev/null

You first assign STDOUT to a file (STDOUT -> file) 
You then assign STDERR to STDOUT (STDOUT -> file; STDERR -> file) 
You then assign STDOUT to /dev/null (STDOUT ->/dev/null; STDERR -> file)

You can't write to the file, so bash's STDERR still goes to STDERR.

SaintHax
  • 1,875
  • 11
  • 16