0

When setting the value of the primary command prompt (PS1) the followig two cases

export PS1="\u"
export PS1="\\u"

gives the same result:

root

as a command prompt string. How does the \u and \\u differs if the both results is identical? Shouldn't the \\u outputs just \u since \\ denotes backslash itself?

Ringger81
  • 1,397
  • 3
  • 13
  • 28

1 Answers1

1

In bash's double-quoted strings, backslashes are preserved if the following character does not need escaping (only " ` $ \ must be escaped).

  • For example, foo="\$bar" will result in $bar because $ needs to be escaped.
  • However, foo="\%bar" will result in \%bar because % does not need to be escaped.

So both PS1="\u" and PS1="\\u" will result in $PS1 having the value \u.


The code \u inside $PS1 is expanded to your username much later – not when assigning the variable, but every time when the prompt is shown.

grawity
  • 501,077