how can I resolve a (shell) back-quoted string in python?
Say, I have the string "`cat /etc/hosts | grep hostname`", and I want to get the shell interpretation of it, e.g: “0.0.0.0 hostname\n”. How can I do that?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use
shlex.split():If, however, you are looking for the output of the command in backticks, you’d need to use the
subprocessmodule instead:Note that I set
shelltoTrueto have the shell interpret it for me.