Bash script printf colors from a variable?

I was hoping to find out if anyone could indicate what I'm doing wrong, because I'm at a dead end. I am trying to learn a bash script to deploy a dev test server. One of the things I'm trying to achieve is to output status outputs when part of the script completes or the action completes. As an example:

    printf "[ %s ] %s distribution detected. Validated as supported by this script.\n" "$PASSED" "$LINUX_DISTRO"

The problem I am facing is formatting the value of the first line %s, which $PASSED. At the beginning of the script, I defined $PASSEDas

PASSED="\e[32mPASSED\e[0m"

However, when I go to execute the script, the output is as follows:

[ \e[32mPASSED\e[0m ] CENTOS distribution detected. Validated as supported by this script.

Instead of the correct output, which looks like this:

[ PASSED ] CENTOS distribution detected. Validated as supported by this script.

Where "PASSED" is written in green.

However, I noticed that if I do the following:

printf "[ $PASSED ] %s distribution detected. Validated as supported by this script.\n" "$LINUX_DISTRO"

, , . ?

+4
2

, bash . % b % s printf. .

printf "[ %b ] %s distribution detected. Validated as supported by this script.\n" $PASSED $LINUX_DISTRO

, .

PASSED="\e[00;32mPASSED\e[0m"
+1

() , escape- , \e printf .

PASSED=$'\e[32mPASSED\e[0m'
printf '[ %s ] some test\n' "$PASSED"

, script /bin/bash, /bin/sh ( bash, , POSIX).

%b : bash, , bash , script.

POSIX- , printf PASSED.

# \e not supported by POSIX; use \033 (octal) instead.
PASSED=$(printf '\033[32mPASSED\033[0m')
printf '[ %s ] some test\n' "$PASSED"
+1

All Articles