Python subprocess with apostrophes, removes them Python subprocess with apostrophes, removes them docker docker

Python subprocess with apostrophes, removes them


You can't nest double quotes like that. Assuming we can correctly guess what you are attempting to say, try

subprocess.run([    'docker', 'exec', 'hbase', 'bash', '-c',    '''echo 'create "myTable", "R"' | hbase shell'''])

which also conveniently gets rid of the pesky shell=True. (I also took out the -e from echo -e which didn't seem to be contributing anything useful. Probably prefer printf if you actually need that feature, anyway.)

Python provides triple quotes specifically so that you have a way to pass in both single and double quotes in a string if you need to. The shell accepts either single or double quotes around a string (though they have slightly different semantics -- single quotes quote completely verbatim, while double quotes are somewhat weaker) and so we use those around the argument to echo. Not quoting the argument would cause bash to parse and remove the quotes. (In the shell itself, you could equivalently say echo "create \"myTable\", \"R\"" inside double quotes, with the literal double quotes escaped; but getting that through two additional layers of backslash quoting is going to get ugly. Both Python and bash -c want to do things with those backslashes.)