title: Bash string parsing, skewed by internal strings
body: <p>I have a string in my bash shell like:</p>
  
  <p><code>out=$(su - user -c "someCommand -f 'string text "problemString"'")</code></p>
  
  <p>The problem here is that it's getting parsed as so:</p>
  
  <p><code>out=\$(su - user -c \"someCommand -f 'string text \"problemString\"'\")</code></p>
  
  <p>I don't want <code>"problemString"</code> to be parsed out -- i.e., it needs to stay exactly as-is, including the quotes. How can I do that?</p>
  
  <p><strong>Update:</strong> I've attempted to escape the inner <code>"</code> with:</p>
  
  <p><code>out=$(su - user -c "someCommand -f 'string text \"problemString\"'")</code>, </p>
  
  <p>but when the command is executed on the host machine, it returns an error from <code>someCommand</code>: </p>
  
  <p><code>Unknown command '\p'</code></p>
  
  <p><strong>Update 2:</strong></p>
  
  <p>Real example:</p>
  
  <p><code>OUTPUT=$(su - mysql -c "mysql --skip-column-names --raw --host=localhost --port=3306 --user=user--password=pass -e 'show variables where variable_name = \"max_connections\"'")</code></p>
  
  <p>I'm passing this bash script via fabric in Python:</p>
  
  <pre><code># probably not relevant, but just in case..
  def ParseShellScripts(runPath, commands):
      for i in range(len(commands)):
          if commands[i].startswith('{shell}'):
              # todo: add validation/logging for directory `sh` and that scripts actually exist
              with open(os.path.join(runPath, 'sh', commands[i][7:]),"r") as shellFile:
                  commands[i] = shellFile.read()
                  print commands[i]
      return commands
  </code></pre>
  
  <p>This prints: </p>
  
  <p><code>OUTPUT=$(su - mysql -c "mysql --skip-column-names --raw --host=localhost --port=3306 --user=pluto_user --password=pluto_user -e 'show variables where variable_name = \"max_connections\"'")</code></p>
  
  <p>which then gets executed on some remote box via fabric, which results in <code>ERROR at line 1: Unknown command '\m'.</code></p>