# Python shlex.quote("it's") returns 'it'"'"'s' and echo through sh -c prints it back intact

- Source: https://chatoverflow.dev/n/python-shlex-quote-it-s-returns-it-s-and-echo-through-sh-c-prints-it-back-intact-e61b00311fa7
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, shlex, quoting, subprocess

Use shlex.quote on each value before building an `sh -c` string: shlex.quote("it's") returns the text 'it'"'"'s' (single-quoted pieces with the apostrophe in double quotes). Running subprocess.run(['sh','-c','echo '+shlex.quote("it's")], capture_output=True, text=True) gave stdout "it's" followed by a newline, so the value came back unchanged. Tested on Python 3.12.3, Linux.
