5 Answers
5

It's an awkward way to check if PS1 is bash's default, or if it was customized by the user in ~/.bashrc (or in any of the /etc/profile, /etc/profile.d/* or ~/.bash_profile files in the case of a login shell).

In the former case, PS1 will be set to the ugly and annoying [user@host last_path_elem]$; in the latter case, it will be left as is.

Notice that on RedHat /etc/bashrc is sourced explicitly from the default ~/.bashrc and ~/.bash_profile installed from /etc/skel; do not confuse /etc/bashrc with /etc/bash.bashrc which is sourced before ~/.bashrc on some systems (eg. Debian).

(I debated whether to make this an answer since it's such a swag but here ya go...)

Just a theory but the first form (e.g. bash-3.2.1$) looks like the prompt I often see when logged in as root or other "non-user" account . The second, overriding form (e.g. [joeblow@myhost /tmp]$) is more user-centric. So maybe this is to detect when going from a system account to user account...then, and only then, change to a more appropriate prompt. Otherwise assume someone would want to preserve the current prompt.

The check being done is a string comparison =. It's comparing the value of $PS1 to the string \s-\v\$, where the backslashes need to be escaped in the string so that they get compared to actual backslashes instead of attempting to escape the following character.

The && syntax is the part that sets PS1 only if the preceding test succeeds.

The overall logic here is apparently to update PS1 only if it was previously set to a specific value.

I understand that it is string comparison and && being used as a test. My question rather was about significance of that string and "why is PS1 set only if the test succeeds".
– AkilanAug 10 '17 at 11:45

The poster already acknowledged that && was testing for the success of the previous command and so the answer doesn't give the required response,
– Raman SailopalAug 10 '17 at 12:52

I think you'd have to ask the maintainers of that file to ask why it is set that way.
– Jeff Schaller♦Aug 10 '17 at 22:49

\s-\v\$␣is the default prompt string, the one Bash sets when starting interactively with PS1 unset. It shows the basename of the shell process (usually bash or sh), the version and a dollar sign, e.g. bash-4.4$. (Or a # instead of $ if running as root.)

The idea of appears to be to set a more useful prompt, but not if some other startup file has already set it.

The backslashes are doubled in "\\s-\\v\\\$ " since within a double-quoted string \x has a special meaning for some values of x. \\ unambiguously represents a literal backslash. (Though for some reason they haven't done that on the assignment side).

Using single quoted strings would make the backslashes a bit easier to read:

Bash unsets PS1 completely if the shell is non-interactive, so testing it against a non-empty string value doesn't really help there. Testing for empty $PS1 is done in some places, e.g. Debian's /etc/bash.bashrc has [ -z "$PS1" ] && return to exit if the shell is non-interactive.
– ilkkachuMar 15 at 23:39