LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 10-25-2006, 02:49 AM   #1
Suinatsa
Member
 
Registered: Jul 2004
Distribution: Slackware, Suse
Posts: 46

Rep: Reputation: 15
printf escaping problem


Hey guys,

I have a little problem with a printf command.

With printf you have the option to print the ASCII character defined by a octal value.

To do that you type in printf"\nnn" where nnn is the octal value.
Now I have a script which does that but my nnn is a variable. My syntax is

$TEX=`printf "\$NUMBER"`

and that does not work. When I use

$TEXT=`printf "\133"

it works and echo $TEXT gives me [.

How can I escape my variable so that it is substituted?

THX

Suinatsa
 
Old 10-25-2006, 03:08 AM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
I think you escape the wrong 'thing'. Your number should already contain the backslash.
Code:
text="\133"
printf $text
printf "\n"
 
Old 10-25-2006, 03:25 AM   #3
Suinatsa
Member
 
Registered: Jul 2004
Distribution: Slackware, Suse
Posts: 46

Original Poster
Rep: Reputation: 15
Yeah you are right. Now it works

Thanks again.

Suinatsa
 
Old 10-25-2006, 09:28 AM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Alternatively, you can do this:
Code:
#!/bin/bash

text=133

printf "\\${text}\n"
I'll try to explain what's going on if you're interested...

The problem you're running into is which program interprets the backslash and when. In your original example, the single backslash is interpreted by bash to mean a literal $, because of that bash does not substitute the value of the variable, and then passes the following text to printf to: "${text}"

Wim's example gets around this by putting the backslash in the variable itself. As mentioned above, bash interprets backslash/escape sequences before variable substitution. So by the time the variable ${text} is replaced with "\133" bash won't interpret the backslash as an escape sequence. Consequently, bash gives printf "\133" to work with.

My example works because it anticipates the bash interpretation. It adds the extra backslash to tell bash "Trust me... I really want a literal backslash rather than an escape sequence." So bash replaces the two backslashes with a single one, performs variable substitution, and then passes "\133" to printf.

Knowing which program will interpret/modify a string can be a constant source of frustration. It still happens to me all the time.
 
  


Reply



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
printf problem??? Mistro116@yahoo.com Programming 2 11-13-2005 08:45 PM
tcsh escaping ' or $ smk123 Programming 3 10-29-2005 03:35 PM
javascript escaping / rblampain Programming 1 09-19-2005 07:16 AM
Escaping from .bash_history ivanatora Linux - General 2 04-25-2004 04:37 PM
escape characters not escaping BobNz Linux - Software 2 04-09-2004 03:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:27 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration