Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash prompt in OS X terminal broken

Tags:

bash

unix

macos

I am using bash in os X Terminal app, and my custom $PS1 breaks when I scroll through my history.

PS1="${BLUE}\u${CYAN}@${RED}\h${BLUE}\w\n\[${red}\$${NC}\]"

also tried PS1="${BLUE}\u${CYAN}@${RED}\h${BLUE}\w\r\n[${red}\$${NC}]"

The problem seems to be in the newline. I have used this bash prompt on Slackware no prob.

like image 701
Milhous Avatar asked Sep 19 '08 20:09

Milhous


People also ask

Where is PS1 set in Mac?

But default value for PS1 in Mac OS is set in /etc/bashrc . It's done conditionally--i.e if PS1 exists (it's a login shell) then PS1 is assigned a value.

What is export PS1?

export is used to set environment variable in operating system. This variable will be available to all child processes created by current Bash process ever after. PS1 is the primary prompt which is displayed before each command, thus it is the one most people customize.


2 Answers

You need the [ and ] arond every escape sequence; do $BLUE and the like include these? If not, they need to be bracketed with these calls.

like image 117
Ben Stiglitz Avatar answered Sep 23 '22 05:09

Ben Stiglitz


To avoid such 'escaping' difficulties as you prompt needs evole to be more complex, this should be a skeleton to start growing on:

function _my_prompt ()
{ 
  # magic goes here
  my_prmpt=.... 
}
PROMPT_COMMAND='_my_prompt'
PS1="[\$my_prmpt] \$"
like image 44
Hedgehog Avatar answered Sep 23 '22 05:09

Hedgehog