Search code examples
pythonbashformatsubprocessripgrep

python different output when run on bash and subprocess


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


Solution

  • 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.