In python console:


In bash console:

echo -e 'xx\nyy'

In mysql console:

create database mytest;
use mytest;
CREATE TABLE sample(content VARCHAR(250));
insert into sample(content) values('xx\nyy');
select content from sample;
| content |
| xx
yy   |

All of them treat the literal \n as newline instead of literal \n.

cat > mytest.txt <<EOF

Open mytest.txt with vim or leadpad ,you will find the fact that vim and leafpad treat literal \n as literal \n ,instead of a newline. Is there a editor tool treating literal \n as newline?


\n is how Linux/Unix expresses "newline", unless you mean the two characters \ and n. That's how the character sequence is typed in, through a text editor. If you want the two character sequence \n in your string, rather than "newline", you'll have to escape the \ (make the \ not be special), by putting a \ in front of it


is the 6 character string x, x, \, n, y, y.

| improve this answer | |
  • This is wrong. In a shell, \n is not treated specially when it appears in "xx\nyy". The only form of quoting in Bash that treats \n as a newline is $' '. "xx\\nyy" also works, as \\ in double quotes is treated specially, but one need not write that (and 'xx\nyy' is clearer). echo -e (which the OP used) treats \n specially, and various other commands and languages do, but this doesn't happen in most circumstances. Also the beginning of this answer seems to say \n in a text editor is actually saved as a newline character, which is not true, but perhaps you don't mean that. – Eliah Kagan Aug 9 at 17:47

TL;DR: The trend to treat \n as a newline is not universal and, when it is treated as a newline, the way to avoid that (especially the best way) differs based on context. Shells don't actually treat it as a newline, so with echo, just don't pass -e. In other languages, consult the language's documentation.

The premise of this question is not correct. \n is not actually taken to represent a newline in most circumstances. Some programming languages (and database query languages) treat \n specially in some circumstances. This is quite widespread but far from universal.

When \n is taken to represent a newline, this happens due to specific rules of specific languages, so there is no fully general way to suppress it. In particular, where it is not treated specially at all, attempting to suppress it will usually end up with something else that you don't want instead. In many situations, writing \\n as waltinator has suggested will work, but in many others it will not, and even in the situations where it does work, it often is not the clearest or easiest way to express a literal \n.

In particular, shells do not generally treat the sequence \n to mean a newline. You could make Bash do this by using the special $' ' form of quoting. That is, $'\n' is treated as a newline character. In Bash and other Bourne-style shells, in both single quotes and double quotes, \n is left alone.

  • '\n' becomes simply \n because no character except ' is treated specially after an opening '--backslashes are never treated specially in single quotes, in your shell.
  • "\n" becomes simply \n because \ is selectively treated specially in double quotes, but the sequence \n has no special meaning, so this is not done.
  • Outside of quotes, \n becomes simply n, because the \ is treated specially, but the sequence \n has no special meaning, so it just becomes n after quote removal.

'xx\nyy', as you have used, is by far the clearest way to express the literal string xx\nyy in Bash, due to the very simple rule that everything between an opening and a closing single quote is treated literally. I recommend against using any other form of quoting for this. It can be done, but it doesn't improve anything. In your example, it is echo -e that is performing the transformation.

The reason you got a newline from echo -e 'xx\nyy' is that:

  • The shell treats \n literally in 'xx\nyy', which becomes xx\nyy after quote removal. Thus the second argument passed to echo is xx\nyy.
  • echo itself treats \n to mean a newline because you passed the -e option. If you don't want that, just don't pass -e.

I should note that echo is not very portable. One of the way it's not is that some echo implementations, on some systems and in some shells, do automatically behave as echo -e does in Ubuntu. (So for scripting, it's generally best to prefer printf.) However, the echo implementations one is likely to use on Ubuntu are Bash's echo builtin, and GNU Coreutils echo, which provides /bin/echo (which may be resolved as /usr/bin/echo on newer systems where /bin and /usr/bin are merged). Both of these echos behaves as I've described: backslash sequences such as \n are printed literally unless the -e option is used.

As for \n in whatever programming language or database query language you're using, the general answer is that you should consult the language's documentation. In your Python example, the \n in 'xx\nyy' is taken to mean a newline. Although you could write 'xx\\nyy' (Python does support that), it is often clearer to instead simply suppress all backslash sequences by prefixing r to make it a verbatim string literal: r'xx\nyy'.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.