How To call a bash command with variables in Python

Updated October 2026: the code in my 2020 version of this post didn’t run. It used : where it needed =, wrote Import with a capital, and passed a command string to Popen without a shell, which raises FileNotFoundError. I’ve rewritten the examples, run every one on Python 3.14.4, and added the part I should have warned you about, which is shell injection. A quick Google of the title only gave me answers for the inverse, calling a Python script from bash and handing it variables. At least the first three results did, and I’m probably not alone in scrolling past the Stack Overflow answers. ...

December 28, 2020 · 6 min · Tom Cocking