How to escape escaped quote in bash

escape double quotes bash
bash escape single quotes
bash escape single quote in variable
bash replace quote with escaped quote
bash escape backslash
bash escape single quote in double quotes
bash escape dollar sign
bash escape online

I try to run python code with "c" parameter:

python3 -c $'import sys\nfor r in range(1): print(\'rob\')'

It's worked, prints

rob

But i want to print quote:

I'm a rob

This not worked of course:

python3 -c $'import sys\nfor r in range(10): print(\'I'm rob\')'

How to achieve that?

How would you write that string in Python? If you wanted to keep it in single quotes, it would be something like:

print('I\'m rob')

...so that would become, inside a $''-quoted string, something like:

print(\'I\\\'m rob\')

...once you escape both the quotes and the literal backslash parsed by the Python interpreter as protection for the internal quote. Thus:

python3 -c $'import sys\nfor r in range(10): print(\'I\\\'m rob\')'

By the way -- there's no good reason to do this by hand when you can ask the shell to do it for you:

script=$(cat <<'EOF'
import sys
for r in range(10):
  print('I\'m rob')
EOF
)

printf '%q ' python3 -c "$script"; printf '\n'

...emits:

python3 -c $'import sys\nfor r in range(10):\n  print(\'I\\\'m rob\')'

...which you can actually run as code. (Or you could just run python3 -c "$script" after the above heredoc-based definition, which will invoke your script without ever needing to involve a hand-escaped copy of the code anywhere).

Double Quotes (Bash Reference Manual), The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Simple example of escaping quotes in shell: From the man page of bash: A single quote may not occur between single quotes, even when preceded by a backslash.. Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard.

Using escaped double quotes in the print should work:

python3 -c $'import sys\nfor r in range(10): print(\"I\'m rob\")'

The real difference between 'single quotes' and “double quotes” in , How would you write that string in Python? If you wanted to keep it in single quotes, it would be something like: print('I\'m rob') so that would  3.3.2. Escape characters. Escape characters are used to remove the special meaning from a single character. A non-quoted backslash, \, is used as an escape character in Bash. It preserves the literal value of the next character that follows, with the exception of newline. If a newline character appears immediately after the backslash, it marks the continuation of a line when it is longer that the width of the terminal; the backslash is removed from the input stream and effectively ignored.

As per @pault said but with further explanation.

This isn't working because you're ending the string and starting over by putting the quotes in, that's because you used a single quote to denounce the string. This can be easily remedied by using double quotes to denounce the string.

python3 -c $'import sys\nfor r in range(10): print("I\'m rob")'

How to escape single quote in sed?, You have three mechanisms of escaping available to you: escape characters (the backslash character in bash), double quotes, and single  Per-character escaping is useful in on expansions and substitutions. In general, a character that has a special meaning to Bash, like the dollar-sign ($) can be masked to not have a special meaning using the backslash: echo \$HOME is set to \"$HOME\" \$HOME won't expand because it's not in variable-expansion syntax anymore

If you aren't using any other arguments for the Python script, pass the string as an argument to reduce the amount of nested quoting you need.

python3 -c $'import sys\nfor r in range(1): print(sys.argv[1])' "I'm a rob"

How to escape quotes in shell?, Escape characters: Bash escape character is defined by non-quoted backslash (\). It preserves the literal value of the character followed by this symbol. Normally​  You have three mechanisms of escaping available to you: escape characters (the backslash character in bash), double quotes, and single quotes. Of all of these, the only one that does not have exceptions is the single quote! For example: double quotes will expand certain characters, such as $. A backslash followed by a newline character tells bash that there is more input to be followed, and the backslash is removed from the input.

How to escape escaped quote in bash, There are three recognized types: per-character escaping using a backslash: \$​stuff. weak quoting with double-quotes: "stuff". 5.1. Quoting Variables. When referencing a variable, it is generally advisable to enclose its name in double quotes. This prevents reinterpretation of all special characters within the quoted string -- except $, ` (backquote), and \ (escape).

Bash Bonanza: Quoting and Escaping Part 1, A non-quoted backslash \ is the Bash escape character. expansion will be performed unless an ! appearing in double quotes is escaped using a backslash. Escapingis a method of quoting single characters. The escape(\) preceding a character tells the shell to interpret that character literally. With certain commands and utilities, such as echoand sed, escaping a character may have the

Bash escape quotes – Linux Hint, Double Quotes (Bash Reference Manual) appearing in double quotes is escaped using a backslash. The backslash preceding the ' ! ' is not removed. I you just want to insert a backslash in front of the double quote you could go with sed like this: sed 's/"/\\"/g' $statusfile >> $statusfile_truncated But if there is to be real escaping going on there is other tools and techniques that should be considered.

Comments
  • Yes. But this solution force me to use double quotes.
  • Why is that a problem?
  • Because I already have some python code, and not able to edit it
  • Please include these sorts of things into your future questions. Also this is only part of the answer we're looking for anyways. What exactly do you mean by "some python code" and the fact that you cannot edit it?
  • Why escape the double quotes when they're inside ANSI-C-style quoting? echo $'"' emits " without needing to make it $'\"'.