How do I print backslash followed by newline with printf?

Jonas Dahlbæk

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}"
\
$ 
Byte Commander

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I print backslash followed by newline with printf?

From Dev

How can I make echo or printf in shell ignore a double backslash (\\) and print it as I see it

From Dev

How do I replace a backslash and newline character in sequence using regular expressions in Powershell?

From Dev

How do I replace a backslash and newline character in sequence using regular expressions in Powershell?

From Dev

How do I print output without a trailing newline in Rust?

From Dev

Should I use printf("\n") or putchar('\n') to print a newline in C?

From Dev

How do I get CUDA's printf to print to an arbitrary stream?

From Dev

How do I print out every string in a char** to printf()?

From Dev

How do I get CUDA's printf to print to an arbitrary stream?

From Dev

How can I print a newline as \n in bash?

From Dev

How can I print a newline as \n in bash?

From Dev

How do I find a particular word followed by a space followed by a number?

From Dev

How do I find a particular word followed by a space followed by a number?

From Java

How to print a single backslash?

From Java

How do I write a backslash (\) in a string?

From Dev

How do I enable backslash support in GCC?

From Dev

How do I print a variable in a shell script without altering trailing newline (if there is one)?

From Dev

How do I print a variable in a shell script without altering trailing newline (if there is one)?

From Dev

How do a print a backslash-escaped string to output '\n' in Python?

From Dev

How to insert a backslash and a newline before a pattern?

From Dev

how to input multiple strings followed by newline

From Dev

how to input multiple strings followed by newline

From Dev

Why is Common Lisp's print output preceeded by a newline and followed by a space?

From Dev

How do I match a newline in grok/logstash?

From Dev

How do I replace a newline in Atom?

From Dev

How do I expect a newline in expect script?

From Dev

How do I get a newline in a Ipython output

From Dev

How can I print a newline without flushing the buffer?

From Dev

How can I print a string with newline without flushing the buffer?

Related Related

  1. 1

    How do I print backslash followed by newline with printf?

  2. 2

    How can I make echo or printf in shell ignore a double backslash (\\) and print it as I see it

  3. 3

    How do I replace a backslash and newline character in sequence using regular expressions in Powershell?

  4. 4

    How do I replace a backslash and newline character in sequence using regular expressions in Powershell?

  5. 5

    How do I print output without a trailing newline in Rust?

  6. 6

    Should I use printf("\n") or putchar('\n') to print a newline in C?

  7. 7

    How do I get CUDA's printf to print to an arbitrary stream?

  8. 8

    How do I print out every string in a char** to printf()?

  9. 9

    How do I get CUDA's printf to print to an arbitrary stream?

  10. 10

    How can I print a newline as \n in bash?

  11. 11

    How can I print a newline as \n in bash?

  12. 12

    How do I find a particular word followed by a space followed by a number?

  13. 13

    How do I find a particular word followed by a space followed by a number?

  14. 14

    How to print a single backslash?

  15. 15

    How do I write a backslash (\) in a string?

  16. 16

    How do I enable backslash support in GCC?

  17. 17

    How do I print a variable in a shell script without altering trailing newline (if there is one)?

  18. 18

    How do I print a variable in a shell script without altering trailing newline (if there is one)?

  19. 19

    How do a print a backslash-escaped string to output '\n' in Python?

  20. 20

    How to insert a backslash and a newline before a pattern?

  21. 21

    how to input multiple strings followed by newline

  22. 22

    how to input multiple strings followed by newline

  23. 23

    Why is Common Lisp's print output preceeded by a newline and followed by a space?

  24. 24

    How do I match a newline in grok/logstash?

  25. 25

    How do I replace a newline in Atom?

  26. 26

    How do I expect a newline in expect script?

  27. 27

    How do I get a newline in a Ipython output

  28. 28

    How can I print a newline without flushing the buffer?

  29. 29

    How can I print a string with newline without flushing the buffer?

HotTag

Archive