Ubuntu – change text-color of the output on command prompt

bashcommand linelubuntuPHP

Suppose i've a php file and i want to change text-colour of my output for a console based application…

<?php
$prompt = "What Is Your Name: ";
echo $prompt;
$answer =  "You Entered: " . rtrim( fgets( STDIN ));
echo $answer;
?>

I want to change text-colour of $answer.

Is it possible? If yes, how can I do this?

Best Answer

Now you can use 24-bit true color in terminal in Ubuntu 16.04

enter image description here

  • The foreground escape sequence is ^[38;2;<red>;<green>;<blue>m
  • The background escape sequence is ^[48;2;<red>;<green>;<blue>m
  • <red> <green> <blue> range from 0 to 255 inclusive.
  • The escape sequence ^[0m returns output to default.

See RGB Color Codes Chart

Demonstration of 24-bit true color in a script:

enter image description here

Here is the modified script to produce colored output.

<?php
$prompt = "What Is Your Name: ";
echo $prompt;
$answer =  rtrim( fgets( STDIN ));
echo "\033[38;2;0;102;0m You \033[38;2;255;0;255m Entered: \033[38;2;255;255;0m $answer \033[0m \n";
?>

Sample Output of the above script:

Sample output