Sh - Backslash Escape Characters (Whitespace, Tabs, Ends, End of Line, Newline) - Non-printing characters

1 - About

Text - Non-printing Character (Tabulation, New Line, ...) in bash

3 - Format

Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows:

  • \a: alert (bell)
  • \b backspace
  • \e an escape character
  • \f form feed
  • \r carriage return
  • \t horizontal tab
  • \v vertical tab
  • \\: backslash
  • \' single quote
  • \nnn the eight-bit character whose value is the octal value nnn (one to three digits)
  • \xHH: the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)
  • \cx a control-x character

The expanded result is single-quoted, as if the dollar sign had not been present.

4 - Management

4.1 - Init

var variable=$'TextWithTab\tInBetween'

4.2 - Concat

variable="First"$'\n'
variable=$variable"Second"$'\n'
echo "$variable"
First
Second

4.3 - Echo

The echo command must have its parameter between parenthesis to show the newlines in a variable
# Variable
TEST=$`Hello\nNico`
echo "$TEST"
# or Text
echo -e "Hello\nNico"
Hello
Nico
!!!! Bad/wrong !!!!
# Bad
echo $TEST
Hello Nico

4.4 - Show

4.4.1 - Cat

To see the backslash character, you need to use the cat command.

echo $'Hello\tWorld\vHello \nHello ' | cat -vte

where:

  • for <wrap box>$'Hello\tWorld\vHello \nHello '</note>
  • the cat options:
    • v: show non-printing
    • T: show tabs
    • E: show ends

Output:

Hello^IWorld^KHello $
Hello $

where:

  • ^I is an horizontal tab
  • ^K is a vertical tab
  • $ is an end-of-line

4.4.2 - Sed

Shell Data Processing - Sed (Stream editor)

echo $'\t' | sed 's/ /·/g;s/\t/→/g;s/\r/§/g;s/$/¶/g'

Data Science
Data Analysis
Statistics
Data Science
Linear Algebra Mathematics
Trigonometry

Powered by ComboStrap