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.
\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
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
\ 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 itself treats
\n to mean a newline because you passed the
-e option. If you don't want that, just don't pass
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
/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.
\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
'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: