10

I am accessing two of my Pis via a serial connection on pins 8 and 10. One of them uses a serial to USB connector for my Mac and the other uses an HC-06 which connects to my Android. The console/terminal over serial is very bland and colorless, unlike the ssh console/terminal which, by default, has a bolded and green hostname as well as a bolded and blueish working directory.

I know that it is possible for the serial pins to relay color in the command line, as grep still returns it's results nicely highlighted in red.

How can I get the hostname and working directory to be their colorful selves again?

1
  • If you are partial to color in the terminal and more dynamic completion mechanisms than bash, I'd recommend the fish shell (that bit about "a command line shell for the 90s" is a joke, since it was first released 10 years ago). Its scripting language is not POSIX but as a default interactive shell it is definitely nicer, once you get the hang of things. I use it on the pi and it is fine over ssh; Raspbian has a package (apt-get install fish, then try it with fish).
    – goldilocks
    Jan 10, 2016 at 13:50

4 Answers 4

7

The default bash prompt is set in a system wide file; for a complete explanation of how bash sources its configuration see INVOCATION near the top of man bash. In short, that system wide file is sourced by ~/.bashrc; it is pretty obvious.

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

Evidentally, what's deciding about the prompt is making the wrong decision. You could dig into that and try to override it by pre-setting some variable, but I recommend against fiddling with /etc/bashrc and instead simply define your own prompt after that sourcing in ~/bashrc.

If you search online for bash prompt you will find copious discussions of it, such as the TLDP How-to. There are actually 4 prompts (see man bash again) but the standard one is defined by PS1. This respects standard ANSI escapes; for an explanation of the potential color codes see here. It also uses special escapes to signify things such as the current working directory, user and host name, etc. Those are all defined under PROMPTING in man bash. An example:

export TERM=xterm-256color
export PS1="\[\033[1;38;5;196m\]\h\[\033[1;48;5;11;38;5;17m\]\u\[\033[0;32m\]\w\[\033[0m\]: "

Gives me:

  • Hostname (\h) in bold bright red.
  • Username (\u) in bold dark blue on bright yellow.
  • Current working directory (\w) in green

But your mileage may vary (in fact, I think the usename bg/fg should be the other way around?). Anyway, it may take a few minutes fooling around; once you've settled on something put it at the end of ~/.bashrc and it should apply everywhere 24/7.

You can include newlines (\n) in a prompt, BTW, which is useful if they are long or include the cwd path.

5

I read Walter's answer here and researched more and found:

https://kb.iu.edu/d/acpy which indicated:

TERM=ansi; export TERM does the job, you can add it to your .bashrc

However, I still have grey for black so I looked a bit more and found this:

https://unix.stackexchange.com/questions/93814/cant-apply-brightness-to-terminals-background-color

Leading to a final solution for me:

TERM=konsole-256color; export TERM

Additionally, I recommend checking out akkana's termsize script to resize the terminal as well:

https://github.com/akkana/scripts/blob/master/termsize

1
  • That did the trick for me and the termsize tip works perfectly as well !
    – mpariente
    Dec 23, 2019 at 15:16
2

Just add:

export PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\] \[\033[01;34m\]\w \$\[\033[00m\] '

at the bottom of ~/.bashrc

2

This has nothing to do with using the serial pins to relay color (that type of analog system went by the wayside long ago).

This has everything to do with terminal type.

When connecting using SSH, the default term type is usually something like xterm or xterm-color. When connecting using a serial port to a console the term type is likely to be something does not indicate that it supports ANSI color (older terminals only supported monochrome, settings colors like you want is a later addition).

Check your TERM (term) type from each login. Then change the console one (or hard code in your .profile)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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