How to colorize command output in bash script?
Asked Answered
G

2

4

I'am trying few days to change color output from command in bash script. I tried some workflows with e.g. trap but without success.The only what partially working is this code:

#!/bin/bash

GRN='\e[32m'
CYN='\e[36m'
END='\e[0m'

echo -e "${GRN}Formating Root partition ..${END}"

echo -e "${CYN}"
(set -x ; mkfs.ext4 -L Root -m 5 /dev/sda2) | GREP_COLOR='49;38;5;007' grep --color=always '.*' 
echo -e "${END}"

echo -e "${GRN}Formating Home partition ..${END}"

...

Is there some better way how to do it. Thank you.

What I want is this:

Formating Root partition ..                                    <= Green
mkfs.ext4 -L Root -m 5 /dev/sda2                               <= Cyan
mke2fs 1.45.2 (27-May-2019)                                    <= Grey
Creating filesystem with 9175040 4k blocks and 2293760 inodes  <= Grey
Filesystem UUID: ...                                           <= Grey
...                                                            <= Grey
Formating Home partition ..                                    <= Green
Griselgriselda answered 18/7, 2019 at 10:22 Comment(1)
I think this thread might help you : https://mcmap.net/q/40458/-how-to-change-the-output-color-of-echo-in-linuxColcothar
L
2

You can use the below script, tested on mac and ubuntu.

each function accept two-argument,

  1. for sample string output for example green "will print green"
  2. Print command output for example red "will print Free space in red:" "free -m" free -m

bash script

#!/bin/bash
set +x
function black(){
    echo -e "\x1B[30m $1 \x1B[0m"
    if [ ! -z "${2}" ]; then
    echo -e "\x1B[30m $($2) \x1B[0m"
    fi
}
function red(){
    echo -e "\x1B[31m $1 \x1B[0m"
    if [ ! -z "${2}" ]; then
    echo -e "\x1B[31m $($2) \x1B[0m"
    fi
}
function green(){
    echo -e "\x1B[32m $1 \x1B[0m"
    if [ ! -z "${2}" ]; then
    echo -e "\x1B[32m $($2) \x1B[0m"
    fi
}
function yellow(){
    echo -e "\x1B[33m $1 \x1B[0m"
    if [ ! -z "${2}" ]; then
    echo -e "\x1B[33m $($2) \x1B[0m"
    fi
}
function blue(){
    echo -e "\x1B[34m $1 \x1B[0m"
    if [ ! -z "${2}" ]; then
    echo -e "\x1B[34m $($2) \x1B[0m"
    fi
}
function purple(){
    echo -e "\x1B[35m $1 \x1B[0m \c"
    if [ ! -z "${2}" ]; then
    echo -e "\x1B[35m $($2) \x1B[0m"
    fi
}
function cyan(){
    echo -e "\x1B[36m $1 \x1B[0m"
    if [ ! -z "${2}" ]; then
    echo -e "\x1B[36m $($2) \x1B[0m"
    fi
}
function white(){

    echo -e "\x1B[37m $1 \x1B[0m"
    if [ ! -z "${2}" ]; then
    echo -e "\x1B[33m $($2) \x1B[0m"
    fi
}


green "Green: Formating Root partition .."
white "White: Formating Root partition .."
# pass the second parameter, will be treated as command
yellow "color command output,print ls:" "ls"
red "Red: System Free RAM:" "free -m"
cyan "cyan: awesome..........end..................."
echo -e "mix the color $(purple "Purple: this is purple, will print disk usage" "du -h") Now Yellow: $(yellow "hi Yelow") "

enter image description here

Lengthwise answered 18/7, 2019 at 14:8 Comment(1)
Hi, I'm new to scripting and was trying to use your script but without success. I created a color.sh which I called from my main script by doing: . $color_script_path red "Hello" I also tried adding the echo to a variable as I've done before: message=$(. $color_script_path red "Hello") echo $message But it only seems to work if I copy/paste the functions in to my main script. Could you help me? ThanksEmbosom
C
0

Use a bash command called tput, which allows you to change the background color of a terminal, text color, and more.

tput setaf <color:0-7>

I have made a simple bash script to show how it is done:

tput setaf 0;
echo "No Color."
tput setaf 1;
echo "Color.";
tput setaf 2;
echo "Color.";
tput setaf 3;
echo "Color.";
tput setaf 4;
echo "Color.";
tput setaf 5;
echo "Color.";
tput setaf 6;
echo "Color.";
tput setaf 7;
echo "Color.";
tput setaf 8;
echo "Duplicate Color.";
tput setaf 9;
echo "Duplicate Color.";

Resource: https://www.baeldung.com/linux/terminal-output-color

Hope your project works out!

Croak answered 22/11, 2023 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.