15

Operating a standard bash shell on a server, the PS1 prompt defaults to ending in a $ for non-root users, and # for root.

IE:

ubuntu@server:~$ sudo su
root@server:/home/ubuntu#

Why is this?

1
  • 1
    Historically or more directly?
    – Jeff Schaller
    Jun 23, 2016 at 22:16

2 Answers 2

20

Historically the original /bin/sh Bourne shell would use $ as the normal prompt and # for the root user prompt (and csh would use %). This made it pretty easy to tell if you were running as superuser or not.

# is also the comment character, so anyone blindly re-entering data wouldn't run any real commands.

More modern shells (eg ksh, bash) continue this distinction of $ and # although it's less important when you can set more complicated values such as the username, hostname, directory :-)

4
  • Can you give an example of how # being the comment character prevents someone running real commands if they're blindly re-entering data? Jun 30, 2016 at 15:07
  • 8
    What happens if you cut'n'paste the complete line # reboot - answer... nothing, 'cos the # is a comment. Jun 30, 2016 at 15:09
  • I had a user client who configured his prompt like john->. He used to complain that all my scripts stopped working after two or three days. That would be because he kept pasting > myScript onto his command line. May 24, 2021 at 14:13
  • I had something similar a few days ago. I asked a client to "copy and paste the log into the issue". They copied a log, pasted it into bash, and sent me the output. Every second line was: bash: ...: command not found
    – Stewart
    May 24, 2021 at 17:00
12

Tradition.

The POSIX standard says (my emphasis):

PS1

This variable is used for interactive prompts. Historically, the "superuser" has had a prompt of #. Since privileges are not required to be monolithic, it is difficult to define which privileges should cause the alternate prompt. However, a sufficiently powerful user should be reminded of that power by having an alternate prompt.

See also this answer to virtually the same question on the SuperUser forum.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.