-2

I am trying to get the bash output to a variable and then hide the output afterwards in the command itself.

I could find a way to hide the output using /dev/null 2>&1 at the end of the command like below:

kubectl get deployments -n kube-system | grep minions /dev/null 2>&1

It hides the output, but I need to get its output to a variable and then hide it (so I will still have the output but command wouldn't show the output in terminal)

How can I do this?

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • 1
    Does this answer your question? [How do I set a variable to the output of a command in Bash?](https://stackoverflow.com/questions/4651437/how-do-i-set-a-variable-to-the-output-of-a-command-in-bash) – builder-7000 Dec 11 '19 at 20:24

2 Answers2

0

Like this :

import subprocess

cmd = "kubectl get deployments -n kube-system | grep minions"
ps = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
output = ps.communicate()[0]
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
-1

Look up backquotes in bash. They capture the output of a command without it appearing to the user.

http://www.tldp.org/LDP/abs/html/commandsub.html

JoelFan
  • 37,465
  • 35
  • 132
  • 205