Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

mysql command line color prompt:

I want to add colors to my MySQL prompt:

I have so far in a script (database.sh):

mysql -uroot -hlocalhost -A --prompt="\u@\h:\d> "

I would like root to be red, @ to be blue, localhost to be green and database to be cyan:

root@localhost:database>

Is it possible to do this in my script?

share|improve this question
    
    
I saw it but it does not work –  EscoMaji Mar 5 '12 at 19:06
    
Yeah I think it's possible the same way a in your regular shell prompt. Your best bet is probably some wrapper or a more advanced MySQL-client. –  micke Mar 5 '12 at 19:12
1  
Does your setup support ANSI escape codes? If it does then you can use them in the following way: --prompt=" ^[[1;33mDTHIS PART IS IN YELLOW COLOUR^[[0m \u@\h:\d> " –  Hennes Mar 24 '13 at 22:53
add comment

3 Answers

Don't listen to people who say you can't. Here:

$ alias colormysql=$(echo -e 'mysql --prompt="\x1B[31m\\u\x1B[34m@\x1B[32m\\h\x1B[0m:\x1B[36m\\d>\x1B[0m "')

Then:

$ colormysql -hHOSTNAME -uUSERNAME -pPASSWORD ...
share|improve this answer
    
Can you give some more explanation as to why this works, but you can't just pass ANSI codes? –  Bryan Agee Nov 5 '13 at 21:14
    
$(foo) executes the result of foo, in this case echo's output. The -e to echo expands the \x1b into an escape char. The \x1b[31m is a terminal control code that sets the current color to red. And so forth. \x1b[0m returns the color to the default terminal color. –  Irongaze.com Apr 1 at 17:34
    
Looks nice, but when using home/end, the cursour end of at the wrong position :-( –  Puggan Se Jun 30 at 7:59
    
when testing colors, i recomendate you skip the alias step, for exemple by doing: mysql --prompt="`echo -e '\033[01;32m\\d\033[01;34m >\033[00m '`" –  Puggan Se Jun 30 at 8:01
add comment

It's a quite unfortunate answer, but you cannot.


Regarding the use of ANSI escape sequences, MySQL only allows the following:

You can use the escape sequences “\b”, “\t”, “\n”, “\r”, “\”, and “\s” in option values to represent the backspace, tab, newline, carriage return, backslash, and space characters.


Regarding cmjdmiller's answer, grc only works to display output from MySQL's shell through a "pager".


The best you could do is use rlwrap like this: rlwrap -a -p'GREEN' mysql -uroot -hlocalhost -A --prompt="\u@\h:\d> ". This will not give you fine-grained control however as it colorizes the entire prompt. Also be careful because it displays the password in cleartext.

share|improve this answer
    
This is out of date. There are a host of possible values for the prompt option. See here: dev.mysql.com/doc/refman/5.6/en/mysql-commands.html –  Irongaze.com Apr 1 at 17:30
add comment

http://wynnnetherland.com/journal/a-stylesheet-author-s-guide-to-terminal-colors

This is definitely a Terminal/Prompt Question. This article explains a lot on the topic. I can't help much more without knowing the prompt you are using though.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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