'How should I redirect stderr on commands that with arguement?

I have a shell script I want to do: ./xxx.sh -a 1 -b 2 -c 3 2>/dev/null

However, the 2 is treated as argument

Similerly with the following command echo test 2>aaa.txt

my intention is to direct strerr to aaa.txt, however, I got:

cat aaa.txt
test 2

as you can see 2 is also treated as an argument. How should I redirect stderr on commands that with argument?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source