What does a hash sign (#) at the end of the directory path in a Bash prompt mean?

6,608

It sounds like you were given a root shell. A root shell is typically indicated with a hash sign (#) as opposed to a user shell which is usually indicated with a dollar sign ($).

Whether or not there is a directory name or user name as well depends on the particular shell in use, as a root shell could look like any of the following:

#
/#
/dir#
username:/dir#
username@hostname:/dir#

The last of the above examples is the default prompt in Bash in Ubuntu. The first is the default in the dash shell. Try it with /bin/sh. Of course this is configurable in both.

To open a root shell, the shortcut is:

sudo -i

This opens the root user's configured shell as a login shell, which will usually be bash. It's a shortcut to sudo su -. If you want to open a different shell as root, without modifying the root user's configured shell, you can use the long-winded:

sudo su -s /bin/sh -

Replace /bin/sh with the shell you want to open.

Share:
6,608

Related videos on Youtube

Struan Murray
Author by

Struan Murray

Electrical & Electronics Engineering student, interested in Audio, Radio and C++ programming. Likes old books & technology.

Updated on September 18, 2022

Comments

  • Struan Murray
    Struan Murray over 1 year

    I was in the middle of installing updates for Ubuntu when I came across an error, which allowed me to "Start a new shell to examine the situation". Doing so placed me at the top of my directory structure in the same window but appending # to the end.

    What does the # represent?

    How is this change achieved from the command line?

    • Chai T. Rex
      Chai T. Rex over 5 years
      You mean that the prompt ends with # instead of $?
    • Ray
      Ray over 5 years
      You need to give us a lot more information than this. For example, what updates were you installing (was it a few packages or was it going from one OS version to another) and what was the error that you encountered. Also, I presume you want to know how to fix your system and not how to change the prompt? (BTW, thanks for teaching me the word "octothrope" First time in years of using it that I've seen it called that!)
    • Chai T. Rex
      Chai T. Rex over 5 years
      @Ray the questions are clearly not about fixing anything. What does the octothorpe represent and how is it achieved is pretty clear.
    • Sergiy Kolodyazhnyy
      Sergiy Kolodyazhnyy over 5 years
      Also this: askubuntu.com/q/881998/295286 Basically there's very elaborate ~/.bashrc for each user on debian based systems, which sets the prompt. The leading / means you're in top most aka root directory
    • Ray
      Ray over 5 years
      @ChaiT.Rex I don't know about "pretty clear". I mean, the question does ask what # means. Yes. So the first sentence about an error is irrelevant? Then, at the very least, the question should contain a phrase indicating that the OP "doesn't care" about the error.
    • Struan Murray
      Struan Murray over 5 years
      To clarify, I was setting the scene by mentioning the cause of the change to the octothorpe. I was simply wondering what the difference between it and the dollar sign was. As well as how you would implement switching between $ and # from the terminal. "What does this represent and how is it achieved?"
  • Struan Murray
    Struan Murray over 5 years
    This answers the first part of my question "What does this represent" thank you. If you could answer "how this is achieved" I'll accept this as the answer.
  • Zanna
    Zanna over 5 years
    @StruanMurray this happens when you start a root shell, eg sudo -i. You could also set your PS1 to use # instead of $, but I wouldn't recommend it. However, I don't know how Bash changes the prompt to # when the shell is owned by root. I don't know where to find it in the source code, I can't find it in the Bash manual, I can't find any variable that does it, and I can't find any Stack Exchange question that explains it. So, if that is what you want to know, great question! If you edit your question accordingly I will vote to reopen it - if you don't want to, I might ask it myself.
  • Struan Murray
    Struan Murray over 5 years
    @Zanna Thanks for this, it wasn't exectly how the character was changed in the source code which I wanted to know, though that may be down to my poor question-wording, but you are right in that it may be a really good question to know where those variables are stored or whether they are hard-coded into the bash prompt, feel free to post your own question! You've answered my question with the sudo -i being the "How this is achieved" part of my question, I would accept this as the answer should you post one but understand that the question may be deleted as a similar one has been asked before.
  • Zanna
    Zanna over 5 years
    @StruanMurray Thanks for your comment clarifying what you wanted to know :) thomasrutter may want to edit this answer to include that you can start a root shell with commands such as sudo -i. Your question is unlikely to be deleted as it has a positive score and positively scored answer. "Duplicates" of good quality are not generally deleted - closing them prevents answers being added, but they are considered useful. If you like the idea of the question I suggested, feel free to ask it! I might get round to it some time tomorrow or later in the week, if you haven't done so.
  • thomasrutter
    thomasrutter over 5 years
    Edited my answer accordingly.