21

The escape character (\) can be used to escape end of line, for example.,

echo This could be \
a very \
long line\!

Output:

This could be a very long line!

However, isn't end of line (new line) represented by \n which has two characters? Shouldn't the result of the escape be the literal of \n? For example,

echo $'\\n'

Output:

\n

I am not trying to echo a new line. I am wondering why \ is able to new line character (\n) which has two character instead of just escape the backslash in the new line character and produce the literal of \n.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mike Lee
  • 1,215
  • 3
  • 10
  • 22
  • 1
    http://www.gnu.org/software/bash/manual/bashref.html#ANSI_002dC-Quoting – glenn jackman Jun 03 '13 at 19:29
  • 10
    Hmm, still not sure I understand your question. Perhaps it will help to know that `\n` is not _really_ a newline character -- it is an escape sequence that _represents_ a newline (which is just one character in Linux). The `\ ` at the end of a line escapes the _actual_ newline character that you type in using the `enter` key. – Markku K. Jun 03 '13 at 19:31
  • @MarkkuK, thank you! this actually answers my question. I always thought `\n` is the new line character it self. That's why I don't understand why a backslash can escape two characters. Sorry about the bad phrasing. – Mike Lee Jun 03 '13 at 19:35
  • You're welcome. Sometimes when you don't know what you don't know, it's hard to ask a question that makes sense :) – Markku K. Jun 03 '13 at 19:50
  • 1
    As a technical addendum to @MarkkuK.'s comment, you can see the newline/linefeed character is a single [ASCII](http://en.wikipedia.org/wiki/Ascii#ASCII_control_code_chart) value (`10`) on an ASCII table. – ajp15243 Jun 03 '13 at 19:56
  • 1
    @MarkkuK. You should make your comment an answer, so the OP can accept it. – Ansgar Wiechers Jun 03 '13 at 20:11
  • @AnsgarWiechers, thanks for the suggestion. I have done so, along with some extra info that may be of use to the OP. – Markku K. Jun 03 '13 at 20:23

2 Answers2

28

Actually, \n is not really a newline character—it is an escape sequence that represents a newline (which is just one character in Linux). The \ at the end of a line escapes the actual newline character that you type in using the enter key. You can look at what ASCII values represent different characters using hexdump:

%echo $'\\n'
\n
%echo $'\\n' | hexdump -C
00000000  5c 6e 0a                   |\n.|
00000003

You will notice that echo printed out 3 characters: \ (5c), n (6e), and a newline (0a). You will also notice that on the right hand side of the hexdump output, newline shows up as a ".", because it is considered a non-printing character.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Markku K.
  • 3,840
  • 19
  • 20
  • Close, but if you avoid doubling up the `\` and include the `-n` parameter to `echo` to avoid the implicit newline then you get a newline character. ```sh echo -n $'\n' | hexdump -C ``` Which results in: ``` 00000000 0a |.| 00000001 ``` – Aaron Robson Nov 26 '22 at 17:55
1

Newline is the name given in the UNIX world to a character that ends a line in a line-oriented file (or in a terminal). In the UNIX/Linux world this corresponds to the ASCII linefeed character.

Different systems use different conventions to end lines: Windows uses a sequence of carriage return and line feed, while Mac originally used a single carriage return. This confusion stems from the fact that these were originally commands needed to move a printer's print head to the beginning of a new line.

\n is a conventional way of expressing the end of line character in code, again originally in the UNIX world, more precisely in the C language. Note that when reading a text file C reads a single newline character even on systems where this is really a two character sequence.

Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55
  • In UNIX if a newline is 2 characters, C (or more precisely fread) reads 2 characters. On Windows, if a file is opened as text file, the library will convert the \r\n sequence to \n . Otherwise even on Windows 2 characters will be read. – martinkunev Jun 11 '15 at 15:09
  • In UNIX newline is one character by definition and that's ASCII linefeed or its correspondent in other character sets. It is true that sequences of linefeed and carriage return are read as they are, without translation. – Nicola Musatti Jun 12 '15 at 06:09