2

I am running a script which internally calls a bash command and parses the output accordingly. For example i am calling this command

result = subprocess.check_output("rg result",shell=True)
print(output)

I get this output without any line number or anything

historyChecker.py:        result = subprocess.check_output("rg --help",shell=True)
historyChecker.py:        output = re.search(r'USAGE:',result)

If i run the same command in bash i get a different result

[~/history_checker/code]$ rg result                                                                                                                                               
historyChecker.py
56:        result = subprocess.check_output("rg --help",shell=True)
57:        output = re.search(r'USAGE:',result) 

any idea why this is happening and how we can solve this. Thanks

Matiiss
  • 5,970
  • 2
  • 12
  • 29
Red Gundu
  • 183
  • 2
  • 10
  • so basically you are asking why one "terminal" formats stuff differently than another? – Matiiss Jul 27 '21 at 20:51
  • 1
    Some programs change their output format depending on whether standard output is a terminal or not. – Barmar Jul 27 '21 at 20:51

1 Answers1

6

From the documentation of rg:

-n,  --line-number
Show line numbers (1-based). This is enabled by default when searching in a terminal.

So when you run it in a terminal, this option is enabled. When you use subprocess.check_output(), standard output is connected to a pipe, and this option isn't enabled.

Add the --line-number option if you want them.

result = subprocess.check_output(["rg", "--line-number", "result"])
print(output)

You're also not doing anything that needs shell parsing, so pass the command line as a list and don't use shell=True.

Barmar
  • 741,623
  • 53
  • 500
  • 612