8

I was hoping to see if someone can point out what I am doing wrong because I am stumped. I am trying to learn bash scripting to deploy a dev test server. One of the things I am trying to achieve is outputting status outputs when a part of the script completes or an action completes. As an example:

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

The problem I am having is formatting the value of the first string, %s, which is $PASSED. In the beginning of the script I have defined $PASSED as

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

However, when I go to execute the script, the output looks like the following:

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

Instead of the correct output which looks like:

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

Where "PASSED" is written in green coloring.

However, I did notice that if I do the following:

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

The above behaves correctly, but I believe isn't the correct way of doing it. Any ideas?

2
  • Don't hard-code terminal escapes in strings, until you know what terminal type you are writing to. Not all the world is a VT100! I suggest you use tput or similar - that's what it's for. Commented Mar 2, 2016 at 16:50
  • @TobySpeight I will take a look at tput. Cheers Commented Mar 2, 2016 at 16:55

2 Answers 2

8

You want bash to interpret the backslash characters. Use %b instead of %s in your printf. So instead you should have the following.

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

Also, I modified your variable.

PASSED="\e[00;32mPASSED\e[0m"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I didn't know about %b, this solved the issue.
2

Another (non-standard) solution is to put the escape character directly in the original string, rather than \e for printf to interpret.

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

However, this assumes that your script is run by /bin/bash, not /bin/sh (which might be bash, but run in POSIX-compliant mode).

Using %b has the same problem: it is specific to bash, and you may not want to assume that bash is the shell executing your script.

A fully POSIX-compliant solution is to use printf itself to create the value of PASSED.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.