Home > Enterprise >  bash: colored output do not stop
bash: colored output do not stop

Time:11-12

I'm writing a password validator and after all the tests I need to give an answer to the user if the password is valid or not. If it is valid I will write the password in green color If it is NOT valid I will write the password in red color My problem is that the color does not stop, the rest of the output would be in color as well for example: Your password is NOT valid! ffsdmkl ! Your password must contain a minimum of 10 characters, Your password must contain both alphabet and number --> from the beginning of the password until the end of the sentence it would be colored. How can I fix it guys?

Thanks!!

green='\033[0;32m'
red='\033[0;31m'
if [[ $valid1 == true && $valid2 == true && $valid3 == false  ]]; then
   echo -e "Your password is valid! ${green} ${PASSWORD} ${clear}!"
else
   echo -e "Your password is NOT valid! ${red} ${PASSWORD} !"
   echo $RESULT
fi

CodePudding user response:

Try this:

green='\033[0;32m'
red='\033[0;31m'
reset='\u001b[0m'
if [[ $valid1 == true && $valid2 == true && $valid3 == false  ]]; then
   echo -e "Your password is valid! ${green} ${PASSWORD} ${reset}!"
else
   echo -e "Your password is NOT valid! ${red} ${PASSWORD} ${reset}!"
   echo $RESULT
fi

CodePudding user response:

You need to define the variable clear as well, and use it whenever the text should not be in color anymore. Because clear is also a Bash command, I would change the variable name to something else like reset:

green='\033[0;32m'
red='\033[0;31m'
reset='\e[0m'
if [[ $valid1 == true && $valid2 == true && $valid3 == false  ]]; then
   echo -e "Your password is valid! ${green}${PASSWORD} ${reset}!"
else
   echo -e "Your password is NOT valid! ${red}${PASSWORD} ${reset}!"
   echo $RESULT
fi
  • Related