FAQ
I need to execute an external shell script via subprocess on Linux.


One of the parameters needs to be passed inside double quotes


But the double quotes do not appear to be passed to the script


I am using :


myscript = '/home/john/myscript'
commandline = myscript + ' ' + '\"Hello\"'


process = subprocess.Popen(commandline, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output,err = process.communicate()




if I make the call from another shell script and escape the double quotes it works fine, but not when I use python and subprocess.


I have googled this but cannot find a solution...is there one?

Search Discussions

  • Larry Martell at Sep 8, 2015 at 11:11 am

    On Tue, Sep 8, 2015 at 7:03 AM, loial wrote:
    I need to execute an external shell script via subprocess on Linux.

    One of the parameters needs to be passed inside double quotes

    But the double quotes do not appear to be passed to the script

    I am using :

    myscript = '/home/john/myscript'
    commandline = myscript + ' ' + '\"Hello\"'

    process = subprocess.Popen(commandline, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    output,err = process.communicate()


    if I make the call from another shell script and escape the double quotes it works fine, but not when I use python and subprocess.

    I have googled this but cannot find a solution...is there one?

    Try it with 2 backslashes:


    commandline = myscript + ' ' + '\\"Hello\\"'
  • Chris Angelico at Sep 8, 2015 at 11:13 am

    On Tue, Sep 8, 2015 at 9:03 PM, loial wrote:
    I need to execute an external shell script via subprocess on Linux.

    One of the parameters needs to be passed inside double quotes

    But the double quotes do not appear to be passed to the script

    I am using :

    myscript = '/home/john/myscript'
    commandline = myscript + ' ' + '\"Hello\"'

    process = subprocess.Popen(commandline, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    output,err = process.communicate()


    if I make the call from another shell script and escape the double quotes it works fine, but not when I use python and subprocess.

    I have googled this but cannot find a solution...is there one?

    First off: Can you remove the shell=True and provide the command as a
    list, so it doesn't need to be parsed? If you can, that would be MUCH
    better.


    But if you can't, the simple fix is to use a raw string literal for
    your Hello. The backslashes are getting lost:

    print('\"Hello\"')
    "Hello"
    print(r'\"Hello\"')
    \"Hello\"


    If In Doubt, Print It Out. It's amazing how much you can learn with a
    few well-placed print() calls :)


    ChrisA
  • Loial at Sep 8, 2015 at 11:13 am
    Yep, that did the trick...cheers

    On Tuesday, September 8, 2015 at 12:04:05 PM UTC+1, loial wrote:
    I need to execute an external shell script via subprocess on Linux.

    One of the parameters needs to be passed inside double quotes

    But the double quotes do not appear to be passed to the script

    I am using :

    myscript = '/home/john/myscript'
    commandline = myscript + ' ' + '\"Hello\"'

    process = subprocess.Popen(commandline, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    output,err = process.communicate()


    if I make the call from another shell script and escape the double quotes it works fine, but not when I use python and subprocess.

    I have googled this but cannot find a solution...is there one?
  • Akira Li at Sep 8, 2015 at 11:15 am

    loial <jldunn2000@gmail.com> writes:


    I need to execute an external shell script via subprocess on Linux.

    One of the parameters needs to be passed inside double quotes

    But the double quotes do not appear to be passed to the script

    I am using :

    myscript = '/home/john/myscript'
    commandline = myscript + ' ' + '\"Hello\"'

    process = subprocess.Popen(commandline, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    output,err = process.communicate()


    if I make the call from another shell script and escape the double
    quotes it works fine, but not when I use python and subprocess.

    I have googled this but cannot find a solution...is there one?

    You don't need shell=True here:


       #!/usr/bin/env python3
       from subprocess import Popen, PIPE


       cmd = ['/home/john/myscript', 'Hello'] # if myscript don't need quotes
       # cmd = ['/home/john/myscript', '"Hello"'] # if myscript does need quotes
       with Popen(cmd, stdout=PIPE, stderr=PIPE) as process:
          output, errors = process.communicate()


    In general, to preserve backslashes, use raw-string literals:

    print('\"')
       "
    print(r'\"')
       \"
    print('\\"')
       \"



    '\"' == '"'
       True
    r'\"' == '\\"'
       True

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedSep 8, '15 at 11:03a
activeSep 8, '15 at 11:15a
posts5
users4
websitepython.org

People

Translate

site design / logo © 2019 Grokbase