5

I am having problems correctly escaping a subprocess call

I want to call sed -n "$=" /path/to/file to count the number of lines in a file but fail to do so from python. My code is as follows:

import subprocess

filename = "/path/to/file"

cmd = subprocess.Popen(["sed", "-n '$='", filename], stdout=subprocess.PIPE)
cmd_out, cmd_err = cmd.communicate()
num_lines = int(cmd_out.strip())

print num_lines

I have tried different escaping combinations for "-n '$='" but nothing seems to work.

markz
  • 1,766
  • 2
  • 14
  • 15
  • I think this mental model is helpful to understand: https://stackoverflow.com/a/70668916/247696 – Flimm Jan 11 '22 at 15:00

2 Answers2

4

-n and $= are two separate arguments.

["sed", "-n", "$=", filename]
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

By default, subprocess.Popen has shell=False.

Without invoking the shell, each argument is passed uninterpreted to sed So sed reads arguments as -n '$='

When you run the command sed -n '$=' , shell removes the '' before sending to the command sed.

So your $= should be without quotes

And as specified by Ignacio, -n $= arguments should be separate

cmd = subprocess.Popen(["sed", "-n", "$=", filename], stdout=subprocess.PIPE)
Hemanth
  • 315
  • 1
  • 8