Home > Blockchain >  bash script ask user to enter something in custom text color
bash script ask user to enter something in custom text color

Time:09-16

We can use color codes such as GREEN='\033[0;32m'RED='\033[0;31m' BLUE='\033[0;36m' WHITE='\033[0;37m' RESET='\033[0m' to echo message in different colors. How can we use these codes with the read command?

I would like to do something like below:

read -p "\033[0;36mEnter the name: \033[0m" name

CodePudding user response:

you have to use $'...' around the string for escape sequences to be processed.

read -p $'\033[0;36mEnter the name: \033[0m' name

Or do it when defining the variables

GREEN=$'\033[0;32m'
RED=$'\033[0;31m' 
BLUE=$'\033[0;36m' 
WHITE=$'\033[0;37m' 
RESET=$'\033[0m'

read -p "${BLUE}Enter the name: ${RESET}" name
  •  Tags:  
  • bash
  • Related