I'm trying out the following thing in my terminal:

spark 0 76 100 | awk '{print substr($0,4,3)}'


If I do:

GRAPH=$(spark 0 25 100 | awk '{print substr($0,4,3)}')
printf "%s" $GRAPH

I get:


But looks more like:

how character looks

As you can see, the % appears to be inside a black bar...

How can I get it to print out:

Without the unexpected % in the same line?

  • A couple of comments. (1) What is spark? I’ve never heard of it, and I didn’t find any reference to it in Super User (in a quick search). The more information you can give about your problem and your situation (within reason), the easier it is for us to help you. (2) One of the cardinal rules of testing/debugging/troubleshooting is to change only one thing at a time. Why did you use “25” with printf and “76” without? Do the first four lines of your question (… “76” …) have anything to do with the rest of the question? – Scott Feb 20 '13 at 18:26

The short answer is to do

printf "%s\n" $GRAPH

When you capture the output from a command into a shell variable with $(…) or `…`, the last newline is stripped, and printf "%s" … (unlike echo) does not append a newline by default.  So, when you say printf "%s" $GRAPH, you are getting the next shell prompt on the same line as your printf output.

Secondly, I advise you to change that to

printf "%s\n" "$GRAPH"

to protect against certain funny characters showing up in $GRAPH –– and you are clearly getting funny characters.

Thirdly, you say, “the % appears to be inside a black bar”.  That appears to be inverse video, in which foreground and background colors are reversed.  That may be caused by one or some of the characters in $GRAPH.  If everything else you do until the end of time is in inverse video, even after you’ve done the above two steps, try

printf "%s\n" "$GRAPH"; tput sgr0

tput exercises terminal-specific functionality; in many cases by writing a string.  sgr0 is the short name for the exit_attribute_mode capability, which sets foreground and background colors back to normal (and turns off underline, blink, and whatever other display attributes your terminal supports).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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