Andrew
Andrew

Reputation: 261

Terminal's font color changed after bash script executing

I have such a script:

#!/bin/bash

temp=`inxi -xxx -w`
regex="(Conditions:(.+))(Wind:(.+))Humidity"
[[ $temp =~ $regex ]]
echo ${BASH_REMATCH[1]}
echo ${BASH_REMATCH[3]}

And it works cool, except such a detail - font color has changed, and now whole text became blue. How can I prevent it?

Upvotes: 2

Views: 444

Answers (1)

0x2b3bfa0
0x2b3bfa0

Reputation: 1055

To avoid blue output:

#!/bin/bash

temp=$(inxi -xxx -w | sed -r 's/\x1B\[[0-9;]*[JKmsu]//g')
regex="(Conditions:(.+))(Wind:(.+))Humidity"
[[ $temp =~ $regex ]]
echo ${BASH_REMATCH[1]}
echo ${BASH_REMATCH[3]}

Changes I've made:

  • Backtick (`) is deprecated. Use $(command) instead of `command`.
  • You can remove the color codes from the output of inxi with the sed regex onto I pipe it.

To use blue output correctly:

#!/bin/bash

temp=$(inxi -xxx -w)
regex="(Conditions:(.+))(Wind:(.+))Humidity"
[[ $temp =~ $regex ]]
echo ${BASH_REMATCH[1]}
echo ${BASH_REMATCH[3]}
printf "\e[0m" # Reset the color.
  • Instead of \e you can use \033 or \x1b. All are the same thing.
  • Instead of [0m you can use [39;49;00m.
  • Instead of printf you can use echo -e, but I don't recommend it.

Further reading: http://wiki.bash-hackers.org/scripting/terminalcodes

Upvotes: 1

Related Questions