10

I had these lines:

if [[ $# -eq 0 ]]; then
  printf "$fail_color Error - Function: $function, Line: $line_number \n"
  printf "do_test: Third parameter missing - expected result\n"
  exit 1
fi

This works fine and gives me the intended output of Error - Function: words, Line: 94

I then used ShellCheck and it recommended

printf "$fail_color Error - Function: $function, Line: $line_number \n
             ^––SC2059 Don't use variables in the printf format string. Use printf "..%s.." "$foo".

So I tried changing it to

  printf "%s Error - Function: %s, Line: %s \n", "$fail_color", "$function", "$line_number"

but now the output shows the color code details instead of the color:

\033[31;1m, Error - Function: words,, Line: 94 
,do_test: Third parameter missing - expected result

Related - is there a better way to name the strings other than multiple %s's?

Detail - the color are defined this way:

fail_color="\033[31;1m"
pass_color="\033[32;1m"
color_end="\033[0m"

4 Answers4

10
fail="\033[31;1m"
color_end="\033[0m"
func="foo"  # (renamed this variable to avoid confusion with function keyword)
line_number="42"

printf "%bError - Function: %s, Line: %d%b\n" "$fail" "$func" "$line_number" "$color_end"

Output:

Error - Function: foo, Line: 42

Tested with Ubuntu 11.04 (bash 4.2.8(1)-release), Ubuntu 14.04.1 LTS (bash 4.3.11(1)-release), RHEL 5.1 (bash 3.1.17(1)-release), RHEL 6.0 (bash 4.1.2(1)-release), RHEL 7.0 (bash 4.3.11(1)-release) and Solaris 11 (bash 4.1.9(1)-release)

Cyrus
  • 12,309
4

I like Cyrus's answer, but this syntax also works:

#!/usr/bin/env bash

fail_color=$'\033[31;1m'
color_end=$'\033[0m'
function="foo"
line_number="42"

printf "%sError - Function: %s, Line: %d%s\n" "$fail_color" "$function" "$line_number" "$color_end"

And ShellCheck says "It all looks good!". :)

PM 2Ring
  • 6,633
0

The printf(1) man says:

%b     ARGUMENT as a string with '\' escapes interpreted, except
       that octal escapes are of the form \0 or \0NNN

So, it`s works: example

Paulo Tomé
  • 3,782
Lasiar
  • 1
-2

The variable "$fail_color" contains one too many, or one too few backslashes before the 033, try changing it or removing the "" to have it debackslashified.

\033

is supposed to be the ESC character (ASCII 27 decimal, 033 octal).

Ned64
  • 8,726
  • They work the other way and without them I get $ ./17_number_letter_counts.sh 033[31;1m, Error - Function: words,, Line: 94 ,do_test: Third parameter missing - expected result – Michael Durrant May 25 '15 at 12:01
  • OK, try adding a backslash instead of removing one - it is a little difficult to correct the code without actually seeing it or being able to call it oneself. Please also check the number of backslashes before the [ and ; characters. – Ned64 May 25 '15 at 12:06
  • I tried adding one, e.g. changing fail_color="\033[31;1m" to fail_color="\\033[31;1m" but I got the same error. – Michael Durrant May 25 '15 at 12:22
  • I meant trying also fail_color="\\033\[31\;1m (something like this has worked for me in the past) but I see that you have now found a different solution. – Ned64 May 25 '15 at 17:01