I'm trying to set my PS1 prompt to

/usr/local/src mario

So I've used the following code inside of .bash_profile:

export PS1=" \w \u\[$(tput sgr0)\]"

And I end up with:

 \w \u\[$(tput sgr0)\]


I can use string concatenation to get what I want but that seems very odd that I need to do that.

migrated from stackoverflow.com Jun 23 '16 at 18:44

This question came from our site for professional and enthusiast programmers.

  • Are you sure you're running Bash? I can't reproduce the \w etc. – Kusalananda Jun 22 '16 at 18:56
  • How would I verify? I think you are right. I ran bash and then reran the source on .bash_profile and it seems to have worked. I'm not sure what it was running if it wasn't Bash. Thanks – user3162553 Jun 22 '16 at 18:57
  • 1
    Well, start Bash with bash, try it there, and if that works, your login shell is probably not Bash. Also, check /etc/passwd for the shell entry on the line with your username. – Kusalananda Jun 22 '16 at 18:59
  • Typically, you put the prompt in .bashrc and don't export it. – chepner Jun 22 '16 at 19:01
  • I wrote it up as an answer and expanded on another issue+solution. – Kusalananda Jun 22 '16 at 19:09

Your shell is likely not bash. Change it with chsh.

Also, there's a problem with part of the $PS1 value:

PS1=" \w \u\[$(tput sgr0)\]"

The $(...) will be evaluated when you set PS1, but just that one time. If you would like to execute tput sgr0 every time the prompt displays, use single quotes:

PS1='\w \u\[$(tput sgr0)\] \$ '

(I have also moved the initial space from the start to the end of the value and added \$ which will make the prompt and command line more readable.

You could also set

PS1="\w \u \$ "

and then set

PROMPT_COMMAND="tput sgr0"

to have tput sgr0 execute before displaying every prompt (I don't know if it makes sense to do this as I don't quite know what it does, but the facility for it is there).

Notice that neither PS1 nor PROMPT_COMMAND needs to be exported as it only makes sense to set them for the current shell session.

  • 1
    tput sgr0 is used to reset terminal attributes like display color. PROMPT_COMMAND is executed before the prompt is displayed, and the tput command is presumably used to undo changes made earlier in the prompt (but not shown). – chepner Jun 22 '16 at 19:40
  • @chepner Ah, but now they know about PROMPT_COMMAND in any case, if they would find that useful for something. – Kusalananda Jun 22 '16 at 19:49

Your Answer

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