0

If i'm doing

oidvalue=$(snmpget -v 2c -c public localhost 1234)

This will store the output of that command in oidvalue, however as it is now, it will print some error to the screen. How can I suppress stderr and stdout at the same time store in the variable?

Thanks

2nd EDIT: the below works for my condition:

oidvalue=$(snmpget -v 2c -c public  localhost 1234) > /dev/null 2>&1

marked as duplicate by muru, Ulrich Schwarz, roaima, Prvt_Yadv, JigglyNaga Apr 5 at 15:35

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Your edit shows a command substitution that captures only the standard error stream of the command and discards the standard output stream completely. – Kusalananda Apr 5 at 5:53
  • For future reference, "it will print some error" is not nearly as useful as something like "it will print the error Error: you didn't specify -f so I can't furble." Remember that we can't see what you can see, so telling us everything is important. In your specific case, if you told us the error message we might be able to advise you how to avoid triggering it in the first place, rather than having to discard it. – roaima Apr 5 at 8:56
2

If that command has no silent option, redirect error to /dev/null.

oidvalue=$(snmpget -v 2c -c public localhost 1234 2>/dev/null)
  • Thank you, but turns out what I needed was actually something like this, $(snmpget -v 2c -c public localhost 1234 2>&1 >/dev/null) – user3128077 Apr 5 at 3:17
  • are you sure? this will set oidvalue to error message, stdout will be sucked by /dev/null . 2>&1 has no effect. – dedowsdi Apr 5 at 3:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.