Using Python, I get
$ python3 -c 'print("\\\n")'
\
$
That is, one backslash and one newline, followed by an extra newline inserted by the interpreter.
Using C, compiling the code
#include <stdio.h>
int main(void)
{
printf("\\\n");
return 0;
}
into a file backslash.out
yields
$ ./backslash.out
\
$
That is, one backslash and one newline.
In bash, I get
$ STRING="\\\n"
$ printf "${STRING}"
\n$
What exactly is the bash printf command doing here? What is it doing differently from the python print
or C printf
commands with respect to the escape character \
? And what will I need to put in the variable STRING
to obtain the following output on my terminal:
$ printf "${STRING}"
\
$
In your snippet below, you use "double quotes" around the backslash escapes:
$ STRING="\\\n"
$ printf "${STRING}"
\n$
However, Bash still evaluates some backslash-escapes inside double quotes, so the content of your variable after that is really \\n
, as "\\"
evaluates to \
.
Put the string in 'single quotes' to prevent the shell from touching any of the backslashes:
$ STRING='\\\n'
$ printf "$STRING"
\
$
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments