0

I wrote this script that I use as a login banner. My original script, much like what you see here, was only text color. I've re-tooled the script to add bold text and background color. Since doing that, I can't get it to work. Something is wrong with my syntax in that the script concatenates everything after the $BOLD to the actual output text. I suspect it's something to do with the semicolons but I've tried it 50 different ways without success. What am I missing? I know this should work because I've tested it in the terminal with the following:

#Example of multiple formats in one line
echo -e "\e[1;41;36mHello\e[0m"

The script:

#!/bin/bash

#constants
BOLD="\e[1m";
ENDCOLOR="\e[0m";

#font color selection
Black="30";
DarkGray="30";
Red="31";
LightRed="31";
Green="32";
LightGreen="32";
BrownOrange="33";
Yellow="33";
Blue="34";
LightBlue="34";
Purple="35";
LightPurple="35";
Cyan="36";
LightCyan="36";
LightGray="37";
White="37";

#bg color selection

bgDefault="49";
bgBlack="40";
bgRed="41";
bgGreen="42";
bgYellow="43";
bgBlue="44";
bgMagenta="45";
bgCyan="46";
bgLightGray="47";
bgDarkGray="100";
bgLightRed="101";
bgLightGreen="102";
bgLightYellow="103";
bgLightBlue="104";
bgLightMagenta="105";
bgLightCyan="106";
bgWhite="107";

# prints colored text
prnt () {

    STR=$BOLD";"$3";"$2"m"$1$ENDCOLOR;
    echo -e "$STR";
}

# prnt text textcolor bgcolor
prnt "Current Date: $(date)" $Red $bgWhite;
prnt "User Info: $(who)" $White $bgBlack;
prnt "Server Uptime: $(uptime)" $Blue $bgWhite;

1 Answer 1

1

Set BOLD="\e[1", without the m.

With original code, it outputs like \e[1m;107;31m, but it should be \e[1;107;31m

Sign up to request clarification or add additional context in comments.

2 Comments

Ah shoot, I forgot to take that out :-/ Thanks. Second set of eyes always helps after too long :)
Even better, don't put the \e[ in BOLD or ENDCOLOR. Think of \e[...m as a function, with the semicolon-separated list of numbers replacing ... being the arguments.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.