redirect stderr to dev/null in bash env.

Working in a bash environment, in the following example, how do I direct the error message that putting in an invalid flag (-j for example) would normally produce to dev/null?

while getopts "abcd" opt
do
case "$opt" in
i) a etc ;;
r) b etc ;;
f) c etc ;;
v) d etc ;;
\?) direct actual error message to dev/null

      exit 1 ;;
esac

done

while getopts "abcd" opt
do
    case "$opt" in
      i) a etc ;;
      r) b etc ;;
      f) c etc ;;
      v) d etc ;;
      *) exit 1 ;;
    esac
done

Just exit.

That doesn't work. It still outputs that -j is an invalid option

if you are so particular about the error messages from bash...
then this would do..

bash <yourscript> 2>/dev/null

Playing around...

while getopts "abcd" opt
do
    case "$opt" in
      i) a etc ;;
      r) b etc ;;
      f) c etc ;;
      v) d etc ;;
      \?) echo "$opt" 2>/dev/null ; exit 1 ;;
    esac
done

Else see this link - . Handling Command Line Arguments

I'm afraid neither of those two solutions work. Both still print the error message, with the added bonus of yours producing a question mark as well, Vino.

Okie dokie.. this should do the work

[/tmp]$ cat sniper.sh 
#! /bin/sh

while getopts "abcd" opt 2>/dev/null
do
    case "$opt" in
      a) echo "I" ;;
      b) echo "B" ;;
      *) exit 1 ;;
    esac
done
[/tmp]$ ./sniper.sh -b
B
[/tmp]$ echo $?
0
[/tmp]$ ./sniper.sh -j
[/tmp]$ echo $?
1
[/tmp]$ 

Cheers'