1

This is regarding bash builtin echo. Per Bash documentation the -e option enables interpretation of backslash escapes.
If I execute echo "Total Amount \$500", I see the expected output Total Amount $500. Here I am able to use backslash to esacpe the "$" character without using the -e option. But to escape a new line or tab charcater I am required to have -e option in the echo command like this- echo -e "Total Amount: \n \$500".

0

2 Answers 2

3

The bash manual says the following regarding echo when used with the -e switch:

If the -e option is given, interpretation of the following backslash-escaped characters is enabled.

It specifies "the following backslash-escaped characters" and then lists them:

\a
alert (bell)

\b
backspace

\c
suppress further output

\e
\E
escape

\f
form feed

\n
new line

\r
carriage return

\t
horizontal tab

\v
vertical tab

\\
backslash

\0nnn
the eight-bit character whose value is the octal value nnn (zero to three octal digits)

\xHH
the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)

\uHHHH
the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value HHHH (one to four hex digits)

\UHHHHHHHH
the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value HHHHHHHH (one to eight hex digits)

This can be confusing because these are really more of an escape sequence than an escape character the escape character (\).

Escape Sequence

An escape sequence is a sequence of characters that does not represent itself when used inside a character or string literal, but is translated into another character or a sequence of characters that may be difficult or impossible to represent directly.

Escape Character

In computing and telecommunication, an escape character is a character which invokes an alternative interpretation on subsequent characters in a character sequence. An escape character is a particular case of metacharacters. Generally, the judgment of whether something is an escape character or not depends on context.

The -e option has very little to do with the escape character but allows it to be used in combination with the above sequences in order to have them interpreted with special meaning.

11
  • 2
    True, but this doesn't answer the question. What happens with $? Commented Oct 26, 2017 at 19:32
  • It's escaped normally. Op was confused about what the manual meant by enables interpretation of backslash escapes not about what the escape character does. Also there is no question...
    – jesse_b
    Commented Oct 26, 2017 at 19:37
  • I think I understand now. The escape for the $ character was for bash, not echo command.
    – RKA
    Commented Oct 26, 2017 at 19:47
  • @RaviKumar, I've updated my question again.
    – jesse_b
    Commented Oct 26, 2017 at 19:55
  • 1
    @RaviKumar Yes, that's it. \$ protects $ for bash so that it passes $ to echo, \n tells echo to put a newline instead. Commented Oct 26, 2017 at 20:12
2

There are two steps of string transformation.

  1. First bash parses the command line echo -e "Total Amount: \n \$500" and determines that it needs to invoke a command called echo with two arguments. The first argument is -e. The second argument is the result of expanding the double-quoted literal "Total Amount: \n \$500". Given the rules for double-quoted strings¹, \$ turns into $ and \n stays \n, so the second argument that is passed to the echo command is Total Amount: \n $500.
  2. The echo command, in -e mode, replaces some sequences beginning with backslash into a single character, e.g. it replaces \n by a newline character.

Note that “escaping” means two different things in your question. In the first step, the backslash in \$ “escapes” the $ in the sense that the $ character escapes special processing and remains intact. In the second step, the backslash in \n “escapes” the normal processing to apply some special processing to the n which leads to a newline character.

¹ Backslash followed by one of "$\` becomes just the second character; backslash followed by anything else stays.
² Backslash followed by some lowercase letters becomes a control character; backslash followed by digits becomes the character with that code point in octal; backslash followed by one of xuU and some hexadecimal digits does the same with hexa; backslash followed by anything else becomes just the second character.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .