LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-11-2013, 02:15 PM   #1
waddles
Member
 
Registered: Sep 2012
Posts: 373

Rep: Reputation: 0
printf-ing escape characters


I have tried several approaches to building a single string for printf to display but always end up not displaying the red spaces and blank spaces following the escape sequences and printing the escape sequences instead. Here is my code:
Code:
        str1="SCORE: ["
        str2=$"\033[31m"
        str3=`echo "          " | cut -c $k`
        str4=$"\033[0m"
        sp=$(( 10 - $k ))
        str5=`echo "          " | cut -c $sp`
        str6="]"
        str="${str1}${str2}${str3}${str4}${str5}${str6}"
#        str=$str1+$str2+$str3+$str4+$str5+$str6 didn't work either
        printf "%s" "$str"
How does one use escape sequences in printf above?
BTW I also tried without the "$" in front of the escape code in str2 & 4.

Last edited by waddles; 11-11-2013 at 02:18 PM.
 
Old 11-11-2013, 03:39 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
If you use the format part in printf (this part: "%s") then all the escaping meeds to be done in the formatting part.

If you leave out the formatting part then the escaping can be done in the string.

Have a look at this:
Code:
#!/bin/bash

TXT1="foo"
TXT2="bar"
GREEN='[32m'
RED='[31m'
RESET='[0m'
printf "\e%s%s\e%s %s\e%s\n" $GREEN $TXT1 $RED $TXT2 $RESET

str1="SCORE: ["
str2='\033[35m'
str3="Some text"
str4='\033[0m'
str5="more chatter"
str6="]"

str="${str1}${str2}${str3}${str4}${str5}${str6}"
printf "$str\n"

Last edited by druuna; 11-11-2013 at 04:38 PM. Reason: reset should be RESET.....
 
1 members found this post helpful.
Old 11-11-2013, 03:39 PM   #3
perbh
Member
 
Registered: May 2008
Location: Republic of Texas
Posts: 233

Rep: Reputation: 38
never mind - got it all wrong anyways - sorry

*chuckles*, but now I've got it right!~!
Code:
s1="\033[31m]"
s2="\033[30m]"
s="${s1}${s2}"
echo -en | od -a
0000000 esc   [   3   1   m   ] esc   [   3   0   m   ]
0000014
I guess the secret is the '-e'-switch to 'echo'

Last edited by perbh; 11-11-2013 at 03:49 PM.
 
1 members found this post helpful.
Old 11-11-2013, 03:56 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
@perbh: Yep, when using echo you need to do something like:
Code:
GREEN="\033[32m"
TEXT="Hello World"
echo -e $GREEN$TEXT
But this is about printf, not echo.
 
1 members found this post helpful.
Old 11-11-2013, 11:30 PM   #5
waddles
Member
 
Registered: Sep 2012
Posts: 373

Original Poster
Rep: Reputation: 0
Thanks to ALL!! Nice explaining druuna the positioning of the escape in the formatting I see is the key. I have been attempting to make use of the printf but I fear I may have been run over by formatting in my early days of FORTRAN.
I am glad perbh brought out echo because in my attempt to employ printf I could have made life simpler from my perspective with echo. Thanks again.
 
Old 11-12-2013, 02:17 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,774

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
or:
Code:
ESC=$(printf '\033')
GREEN="$ESC[32m"
echo "${GREEN}Hello, green"
works in [s]stupid[/s] strict shells like dash, too
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
escape characters in Linux apeselis Programming 1 11-11-2010 04:16 PM
[SOLVED] Having trouble with escape characters in C madsovenielsen Programming 5 03-03-2010 06:36 AM
BASH - printf printing escape codes even though I have %0b defined DevonB Linux - Newbie 5 12-23-2009 01:42 PM
Paths and escape characters code-breaker Linux - Newbie 4 07-11-2006 11:24 PM
escape characters not escaping BobNz Linux - Software 2 04-09-2004 03:34 AM


All times are GMT -5. The time now is 01:41 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration