0

I'm having weird issues with Bash displaying colors from a function I wrote. If it was consistent, that'd be one thing, but I recently built 10 identical instances of RedHat (7.4), and 1 of the 10 is having the problem - the other 9 are working fine.

So I have a function that enables me to print color status messages (simplified for brevity):

printStatus  () {
    local COLOUR_BLUE="\e[0;34m"
    local COLOUR_GREEN="\e[0;32m"
    local COLOUR_NORMAL="\e[0;0m"
    local COLOUR_RED="\e[0;31m"
    local COLOUR_YELLOW="\e[0;33m"

    case ${1} in
        "EMPTY")
                printf "[      ]"
                ;;
        "FAIL")
                printf "[ ${COLOUR_RED}FAIL${COLOUR_NORMAL} ]"
                ;;
        "INFO")
                printf "[ ${COLOUR_BLUE}INFO${COLOUR_NORMAL} ]"
                ;;
        "NOTE")
                printf "[ NOTE ]"
                ;;
        "OK")
                printf "[  ${COLOUR_GREEN}OK${COLOUR_NORMAL}  ]"
                ;;
        "WARN")
                printf "[ ${COLOUR_YELLOW}WARN${COLOUR_NORMAL} ]"
                ;;
        *)
                printf "${1}"
                ;;
    esac
}

So when I call it like this in a script:

printStatus FAIL

it always works as expected (FAIL being in red):

[ FAIL ]

If I call it like this:

printf "%s : This is not going to work\n" $( printStatus FAIL )

it works on 90%+ of my builds. When it doesn't work, all I get is (no color):

1

Which screams (to me) that it's a return code somehow, but why? If it was like this 100% of the time it'd make some sense, but it doesn't. And the really weird part is that it's only for red - if I changed the FAIL condition above to use any other color, it works fine with the new color. I can't for the life of me see what is different for those builds that aren't working. Using "set -x" in my script shows everything to be as I expect.

If it means anything, my version of Bash is: GNU bash, version 4.2.46(2)-release.

Thanks in advance.

Andy
  • 1
  • 1
  • Can you try to use **tput** as I showed here to see if it makes a difference : https://stackoverflow.com/questions/53539636/echo-output-is-different-from-expected-formatted-output/53541562#53541562 – Matias Barrios Dec 05 '18 at 19:34
  • Changing the line to this: local COLOUR_RED=$(tput setaf 1) had no impact. I changed every color to the associated tput value, and again - every color works except for red. – Andy Dec 05 '18 at 19:38
  • Without quotes, your command substitution should output *three* lines, since `printf` will get 4 arguments (your format string, `[`, FAIL, and `]`). Something doesn't add up. – chepner Dec 05 '18 at 19:43
  • You are correct - I simplified the function to keep it brief, but in fact spaces within the function are underscores, and I pipe it though a sed command after the printf command. – Andy Dec 05 '18 at 19:44
  • OK, the `sed` command *might* be relevant, no? – chepner Dec 05 '18 at 19:45
  • No, because the problem is still the same. I still get '1' instead of [\_FAIL\_]. I eliminated sed as a potential problem before posting. – Andy Dec 05 '18 at 19:47

0 Answers0