Python has a very convenient function: repr () , which when applied to a string containing empty characters, prints out the representation of that string, which cannot lead to any human misinterpretation of the actual contents of the string.
eg:
$ python -c "print repr(r'''abcde\rfghi\tjklmn\nopqr\bstuv\fwxyz''')"
'abcde\\rfghi\\tjklmn\\nopqr\\bstuv\\fwxyz'
How can I do the same in bash with printf ?
The perfect tool / trick I'm looking for will literally print
'abcd\refjh\bijk'
for the team
printf "abcd\refjh\bijk" | <something>
The goal of this is to improve a test tool that prints the differences between two lines:
http_response_code=$(curl -s --head http://httpbin.org/ | head -1)
assert_equal "HTTP/1.1 200 OK" "$http_response_code"
> failed: strings do not match
> expected: 'HTTP/1.1 200 OK'
> actual: 'HTTP/1.1 200 OK'
As you can see, the current implementation allows the user to understand and explain the reasons for the failure.
Idealism Instead, I would like to have the following conclusion:
> failed: strings do not match
> expected: 'HTTP/1.1 200 OK'
> actual: 'HTTP/1.1 200 OK\r'
Current Attempts:
printf $'\a\b\e\E\f\n\r\t\v\\\'\"' | cat -Aecho $'\a\b\e\E\f\n\r\t\v\\\'\"' | cat -A | sed -r '$!{ N;s/\$\n/\\n/;t sub-yes;:sub-not;P;D;:sub-yes;}'printf $'\a\b\e\E\f\n\r\t\v\\\'\"' | od -c