Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do \b, \f, \n, \r do? What are the differences?

Tags:

escaping

I'm wondering what some of the escape sequences do including the following:

\b backspace
\f form feed
\n new line
\r carriage return
\t tab

Also, what is the difference between form feed, new line, and carriage return? What is the difference between \t and a space? They all look the same...

like image 476
Tao Avatar asked Mar 10 '16 22:03

Tao


People also ask

What is the difference between \r and \n in Java?

\n is a line feed (LF) character, character code 10. \r is a carriage return (CR) character, character code 13. What they do differs from system to system. On Windows, for instance, lines in text files are terminated using CR followed immediately by LF (e.g., CRLF).

What is the difference between \r and \n in C?

They're different characters. \r is carriage return, and \n is line feed.

Is \n and \r same?

\n is used for the newline or linefeed, whereas \r is the carriage return.

What does \r and \n mean?

\n means new line. It means that the cursor must go to the next line. \r means carriage return. It means that the cursor should go back to the beginning of the line.


1 Answers

You can test these escape sequences using printf, for example in a C program or using a Unix/Linux shell such as Bash as shown below.

  • Backspace causes the cursor to move backwards across the previous character
  • Form feed is usually only used with printers to cause the current page to be fed out so the next page is the current one
  • New line (line feed) causes the cursor to move to the next line. On Unix-related systems this also causes the cursor to move to the beginning of that line.
  • Carriage return causes the cursor to move to the beginning of the current line. On systems using Windows (or MS-DOS) or its relatives (and especially its consoles), a combination of carriage return and line feed is used for new lines.
  • Tab causes the cursor to move to the next tab stop position which is often set at intervals of four or eight character positions apart. It is useful for lining up data in columns. Note, however, that string formatting such as that used by printf is much more reliable because use of tabs can still result in staggered columns. See an example at the bottom of this answer.
  • Space moves the cursor one position forward (the opposite of backspace). Note that on terminals (on the screen) space is most often destructive - that is, if a character was in the position that cursor moves across, that character is removed. The same thing happens when a new character other than space is printed in a position where another character existed (as seen in the backspace example below).

The $ represents the shell prompt and should not be typed. It is included in order to differentiate between what you type and what is output. I've included a newline \n at the end of each string so the output occupies its own line.

$ printf 'help\blo - backspace\n'
hello - backspace
$ printf 'two\nlines - newline\n'
two
lines - newline
$ printf 'clocks - carriage return\rsla\n'
slacks - carriage return
$ printf 'Fruit\tColor\tQuantity\ngrapes\tgreen\t100\nbananas\tyellow\t50\n- tabs\n'
Fruit   Color   Quantity
grapes  green   100
bananas yellow  50
- tabs

Please see ASCII Control Characters for more information.

Try this to see how printf formatting can be an improvement over tabs:

$ array=(A B C 'apple pie' 'banana split' 'cherry turnover' 100 200 300)
$ for ((i=0; i<12; i+=3)); do printf '%s\t%s\t%s\n' "${array[@]:i:3}"; done
A       B       C
apple pie       banana split    cherry turnover
100     200     300
$ for ((i=0; i<12; i+=3)); do printf '%-16s%-16s%s\n' "${array[@]:i:3}"; done
A               B               C
apple pie       banana split    cherry turnover
100             200             300

As you can see, the columns aren't staggered in the second version and overall the output is the same width.

like image 135
Dennis Williamson Avatar answered Sep 25 '22 15:09

Dennis Williamson