Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using > /dev/null when setting a variable

So I found this answer and it didn't work for me. Couldn't find anything else.

Using >nul in a variable

What I'm trying to do is prevent output when setting a variable like this.

var=$(/bin/command.out)

If the command doesn't produce an error, then there's no problem. But if it does, it prints it all out to the terminal. I tried adding this, but it just seems to get ignored.

var=$(/bin/command.out) > /dev/null

I've treid putting everything to the right of the = in quotes. I've tried doing the escape character like that guy suggested. No luck.

Thanks.

just realized that link was for batch. I'm working in bash just so that's clear.

like image 669
user2503227 Avatar asked Oct 29 '25 16:10

user2503227


1 Answers

You need

var=$(/bin/command.out 2> /dev/null)

E.g.

$ var=$(not_a_command  2> /dev/null)
$ echo $var

$(uname  2> /dev/null)
$ echo $var
Linux

There are two outputs in Linux:

  • 1> (commonly called STDOUT) which is the output
  • 2> (called STDERR) which is the error output.

It was the error output which was writing the message.

You can find more details from the following chapter of the "Advanced Bash-Scripting Guide":
Chapter 20. I/O Redirection

like image 174
KeepCalmAndCarryOn Avatar answered Oct 31 '25 05:10

KeepCalmAndCarryOn