Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

bash script ask user to enter something in custom text color

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading