Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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"
share|improve this question
    
Take a look at stackoverflow.com/a/5413029/3776858 –  Cyrus yesterday
1  
Please edit your question and show us the contents of the variables and how they're being set. –  terdon yesterday
    
Sure. Added more on how the color variables are set –  Michael Durrant yesterday

3 Answers 3

up vote 3 down vote accepted

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!". :)

share|improve this answer
    
This is the only that has worked for me so far. –  Michael Durrant yesterday
    
@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 yesterday
fail_color="\033[31;1m"
color_end="\033[0m"
function="foo"
line_number="42"

printf "%bError - Function: %s, Line: %d%b\n" "$fail_color" "$function" "$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)

share|improve this answer
    
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. –  Michael Durrant yesterday
    
This code worked for me on Fedora 21:GNU bash, version 4.3.33(1)-release –  somethingSomething 20 hours ago

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).

share|improve this answer
    
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 yesterday
    
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 yesterday
    
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 yesterday
    
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 yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.