Chris G.
Chris G.

Reputation: 25974

How to silence $(command)>/dev/null - within a function, not --silence the sh file

I am calling a command from a script function. I would like it not to print to out, and only to a variable. This prints to out:

MYVAR=$(docker inspect -f {{.State.Status}} $1)

I tried to add &>/dev/null, but sure enough the variable is not set. Is there a middel step like; $(command >MYVAR&>/dev/null)

Update

Just as I am waiting to accept @anubhava very good answer, I found this to explain Difference between 2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>&1

Upvotes: 0

Views: 86

Answers (1)

anubhava
anubhava

Reputation: 785286

What you actually need is to suppress stderr since you're storing command output in a variable by using 2>/dev/null. So use:

myvar=$(docker inspect -f {{.State.Status}} "$1" 2>/dev/null)

Also suggest you to avoid using all caps variable names to avoid chance of overriding an env var.

Upvotes: 3

Related Questions