8

My PS1 variable cannot parsed correctly, it looks like this,

\[\033[01;32m\]\u@\h\[\033[01;34m\] [\w]\[\033[00m\]
\[\033[01;32m\]\u@\h\[\033[01;34m\] [\w]\[\033[00m\]
\[\033[01;32m\]\u@\h\[\033[01;34m\] [\w]\[\033[00m\]mv .bashrc .bash_profile
\[\033[01;32m\]\u@\h\[\033[01;34m\] [\w]\[\033[00m\]source .bash_profile
\[\033[01;32m\]\u@\h\[\033[01;34m\] [\w]\[\033[00m\]
\[\033[01;32m\]\u@\h\[\033[01;34m\] [\w]\[\033[00m\]ls
Applications    Desktop     Documents   Downloads   Google Drive    Library     Movies      Music       Pictures    Public
\[\033[01;32m\]\u@\h\[\033[01;34m\] [\w]\[\033[00m\]vim .bash_profile
\[\033[01;32m\]\u@\h\[\033[01;34m\] [\w]\[\033[00m\]source .bash_profile
\[\e[;31m\][\u@\h \W]$ \[\e[m\]
\[\e[;31m\][\u@\h \W]$ \[\e[m\]
\[\e[;31m\][\u@\h \W]$ \[\e[m\]
\[\e[;31m\][\u@\h \W]$ \[\e[m\]

What is happened? Thanks.

Polaris
  • 366
  • 1
  • 4
  • 13
  • Did you end up with that in `PS1` on purpose? That very much looks like the result of an unfortunate accident. You should not have `mv .bashrc .bash_profile` or `source .bash_profile`, etc.. In fact, you should normally choose just one of the lines `\[\033[01;32m\]\u@\h\[\033[01;34m\] [\w]\[\033[00m\]` or `\[\e[;31m\][\u@\h \W]$ \[\e[m\]`. Personally, I prefer `PS1="\[\e[1;30m\]\D{%R}\[\e[1;34m\] \h:\w> \[\e[0m\]"`. To fix things just choose the prompt you will use and type `PS1="yourprompthere"` on the command line. Then edit `.bashrc` and find `export PS1="...."` and fix there. – David C. Rankin Dec 01 '19 at 03:43
  • @DavidRankin-ReinstateMonica I use command export PS1="\[\e[;31m\][\u@\h \W]\$ \[\e[m\]". But it does not work. – Polaris Dec 01 '19 at 03:55
  • @DavidRankin-ReinstateMonica Not work. Still show those letters ```\[\e[1;30m\]\D{}\[\e[1;34m\] \h:\w> \[\e[0m\]export PS1="\[\e[1;30m\]\D{%R}\[\e[1;34m\] \h:\w> \[\e[0m\]" \[\e[1;30m\]\D{}\[\e[1;34m\] \h:\w> \[\e[0m\]``` – Polaris Dec 01 '19 at 04:04
  • even ```export PS1="\w"``` does not work. My terminal cannot interpret it. – Polaris Dec 01 '19 at 04:07
  • Well, did you change your shell to the Catalina default, zsh? Could it be that you're trying to feed a Bash `PS1` to zsh? I have no idea how zsh interprets prompt strings, though. – Benjamin W. Dec 01 '19 at 04:14
  • I try ```[\u@\h:\w]\$ export PS1='\[\e[31m\]\u@\h:\w\[\e[0m\] ' \[\e[31m\]\u@\h:\w\[\e[0m\]``` which is an example in this link. It still does not work. This is a new mac. I do not whether I should install something or activate PS1. – Polaris Dec 01 '19 at 04:15
  • If you are using `zsh`, then you will need something like `PS1="%F{243}%T %F{27}%m:%~>%f "` Post the output of `echo $SHELL` – David C. Rankin Dec 01 '19 at 04:18
  • Thanks. I don't know the catalina default is zsh instead of bash. I change it to bash. – Polaris Dec 01 '19 at 04:21
  • 1
    No problem, had me scratching my head. Thanks @BenjaminW.. See [zsh - Prompt Expansion](http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html) for full documentation on the zsh prompt. Good luck with `zsh`. I tried for a while, but always came back to bash. – David C. Rankin Dec 01 '19 at 04:23
  • *nod*. zsh is more forgiving, but that's a double-edged sword; using it, it's easy to get into habits where code you write for any other shell has serious bugs. I swore it off more than 15 years ago now. On the other hand, the version of bash Apple is willing to ship is ancient; installing your own is pretty near mandatory. – Charles Duffy Dec 01 '19 at 06:26
  • BTW, in general, shell prompt questions should be on SuperUser or [unix.se]; they aren't exclusive to software development, as our rules require. (Mac-only questions can also go to [apple.se]). – Charles Duffy Dec 01 '19 at 06:28

1 Answers1

20

I came across this issue as well with a brand new Catalina machine. New models seem to default to zsh shell, instead of the traditional bash, to change this:

Go to Users & Groups (unlock to make admin changes) right-click on your admin user, Advanced Options..., change Login shell to /bin/bash

After right clicking on user

This will allow you to utilize bash's prompt string parsing for a nicer prompt - as mac has traditionally supported in the past.

em_
  • 2,134
  • 2
  • 24
  • 39