I am calling a python script, parse_input.py
from bash
parse_input.py
takes a command line argument that has many '\n'
characters in it.
Example input:
$ python parse_input.py "1\n2\n"
import sys
import pdb
if __name__ == "__main__":
assert(len(sys.argv) == 2)
data = sys.argv[1]
pdb.set_trace()
print data
I can see on pdb that `data = "1\\n2\\n"
whereas I want data="1\n2\n"
I saw similar behavior with just \
(without \n
) which gets replaced by \\
How to remove the extra \
?
I don't want the script to deal with the extra \
as
the same input can also be received from a file.
bash version: GNU bash, version 4.2.24(1)-release (i686-pc-linux-gnu)
python version : 2.7.3
Bash doesn't interpret escape characters in regular single and double-quoted strings. To get it to interpret (some) escape characters, you can use $'...'
:
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. Backslash escape sequences, if present, are decoded
as follows:
\a alert (bell)
\b backspace
\e an escape character
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\\ backslash
\' single quote
\nnn the eight-bit character whose value is the octal value
nnn (one to three digits)
\xHH the eight-bit character whose value is the hexadecimal
value HH (one or two hex digits)
\cx a control-x character
The expanded result is single-quoted, as if the dollar sign had not
been present.
i.e.
$ python parse_input.py $'1\n2\n'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With