Issue with Updating Bob's Prompt in Linux Bash Prompt Lab

Question 8 of the Linux Bash Prompt Lab asks to update Bob’s prompt so that it displays the date in the following format:

Example:
[Wed Apr 22]bob@caleston-lp10:~$

Solution: The following command works as expected:
echo 'export PS1="[\d]\u@\h:\w$"' >> ~/.profile

This successfully updates the prompt to display the date (\d), username (\u), hostname (\h), the working directory (\w) and the prompt symbol: $ for regular users and # for the root user.
And the change is made persistent.

However, this alternate solution does not seem to be accepted:
echo 'export PS1="[\d]\u@\h:\w\$"' >> ~/.profile

Possibly due to the separate use of \w and \$. The earlier solution combined these into a single command string.

Could you clarify why the second approach with the separate \w and \$ does not work and suggest any adjustments?

I think there are 2 ways to interpret the question:

\w$:

  • This treats $ as a literal character, not an escape sequence. In this case, it will just print the current working directory (\w), followed by the $ symbol always.

VS

\w\$:

  • The backslash before the $ in \w\$ escapes the dollar sign, meaning that it’s treated as part of the escape sequence. The $ here is used to conditionally display the prompt symbol: $ for regular users and # for the root user.

This will result in:

$ for a regular user
# for the root user

One will be marked correct and not the other because the grader will be searching the .profile file for the string as given in the solution as opposed to testing what the prompt looks like when rendered.

For a direct string compare, [\d]\u@\h:\w$ not equal to [\d]\u@\h:\w\$