僕は普段はbashでは以下のような変数を定義して、カラー出力は簡単にできるようにしています。
red=$'\e[1;31m'
grn=$'\e[1;32m'
yel=$'\e[1;33m'
blu=$'\e[1;34m'
mag=$'\e[1;35m'
cyn=$'\e[1;36m'
end=$'\e[0m'
printf "%s\n" "Text in ${red}red${end}, white and ${blu}blue${end}."
ただこれだと、バッググラウンドの背景色やら、太字やらアンダーラインやらといった高度なことはできません。
まぁ、他にやりようはありそうですが、このchalkはそういうのを簡単にしてくれます。
chalk/chalk: 🖍 Terminal string styling done right
sudo npm install chalk
使用例
yuis@yuis:~/share04/_tmp/tmp$ node
> const chalk = require('chalk'); const log = console.log;
undefined
> log(chalk.blue('Hello') + ' World' + chalk.red('!'));
Hello World!
undefined
>
log(chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz'));
// Nest styles
log(chalk.red('Hello', chalk.underline.bgBlue('world') + '!'));
// Nest styles of the same type even (color, underline, background)
log(chalk.green(
'I am a green line ' +
chalk.blue.underline.bold('with a blue substring') +
' that becomes green again!'
));