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

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:


4 Answers 4

func="foo"  # (renamed this variable to avoid confusion with function keyword)

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


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)

  • This didn't work for me. Maybe becuase I am using an include file for the color codes. However PM 2Ring's answer did work for me. May 25, 2015 at 13:37
  • This code worked for me on Fedora 21:GNU bash, version 4.3.33(1)-release May 25, 2015 at 22:42

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

#!/usr/bin/env bash


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

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

  • This is the only that has worked for me so far. May 25, 2015 at 13:38
  • @MichaelDurrant Thanks! But it's odd that Cyrus's code didn't work. What version of bash are you using; FWIW, I'm using 4.1.5(1), with a ShellShock patch.
    – PM 2Ring
    May 25, 2015 at 14:11
  • @PM2Ring this solution is not working with shell. Can you please assist?
    – ekassis
    Oct 22, 2017 at 14:58
  • 1
    @ekassis "not working" is very vague. What does it do? Are you using Bash? If so, which version? What terminal are you using?
    – PM 2Ring
    Oct 22, 2017 at 15:05
  • 1
    Note unix.stackexchange.com/q/371827/5132 of course.
    – JdeBP
    Dec 18, 2019 at 13:06

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


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.


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

  • 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 May 25, 2015 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, 2015 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. May 25, 2015 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, 2015 at 17:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .