[bash]printf octal instead of decimal


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [bash]printf octal instead of decimal
# 1  
Old 04-14-2009
[bash]printf octal instead of decimal

Hello everybody,

I would like to understand why the printf function is returning me an octal value with this command :

printf %4.4d 0010 returns 0008

printf %4.4d 10 returns 0010

Thanks for help.
# 2  
Old 04-14-2009
Because of the '0' at the beginning. This usually tells the system to interpret this as an octal value, much like 0x starts a hex number.
# 3  
Old 04-14-2009
Allright, but removing the zeros is not the solution i m looking for.
Is there a way to tell printf to interpret it as decimal value ?
# 4  
Old 04-14-2009
The obvious question, then, is: why not?
Code:
$ var1="0011"
$ echo $var1
0011
$ echo $var1 | sed -e 's/^0\+//'
11
$ printf "%04d\n" $( echo $var1 | sed -e 's/^0\+//' )
0011
$ var2=$( printf "%04d\n" $( echo $var1 | sed -e 's/^0\+//' ) )
$ echo $var2
0011
$ var2=$( printf "%05d\n" $( echo $var1 | sed -e 's/^0\+//' ) )
$ echo $var2
00011

Pretty simple, isn't it? Other than that (or using bc instead of sed) I know no way to "force" printf on how to interpret the argument.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use printf in bash

Hello, this is my first time on the forum. I seem to not have the ability to start a new thread. However, I have a very simple question. I am trying to write a script I have to use printf to say "My name is myname (pulled from $USER) . it works with echo, but with printf the $USER... (2 Replies)
Discussion started by: sheltie042
2 Replies

2. Shell Programming and Scripting

How to compare decimal values in bash?

Hi, I am facing some error while doing the comparision between 2 decimal values in bash. Pl help me out. I have tried using below scripts. But its giving me error. 1)amt=12.3 opn_amt=12.5 var=$(awk 'BEGIN{ print "'$amt'"<"'$opn_amt'" }') if ;then echo "correct" else echo "Wrong"... (3 Replies)
Discussion started by: Siba Tripathy
3 Replies

3. Shell Programming and Scripting

Printf question: getting padded zero in decimal plus floating point together.

Hi Experts, Quick question: I am trying to get the output with decimal and floating point but not working: echo "20.03" | awk '{printf "%03d.2f\n" , $0 }' 020.2f How to get the output as : 020.03 Thank you. (4 Replies)
Discussion started by: rveri
4 Replies

4. Shell Programming and Scripting

Bash Rounding to 2 decimal places

I have a number in a bash variable n, and want to round it to 2 decimal places. How can I do that? n=0.0867268 Need to have num=0.09 (1 Reply)
Discussion started by: kristinu
1 Replies

5. UNIX for Dummies Questions & Answers

Using printf in bash

printf "%5.5\n" "1234567890" will print 12345 . How do I get it to print 67890 Essentially, I just want the last 5 characters rather than the first 5. (4 Replies)
Discussion started by: lavender
4 Replies

6. Shell Programming and Scripting

bin bash decimal compare

I need decimal comparing with if. Check if apache version is less than 2.2.17. I tried this and not working. #!/bin/bash apachever=`/usr/local/apache/bin/httpd -v | head -1 | awk '{print $3}' |cut -d/ -f 2` if ]; then echo "Apache version less than 2.2.17" else ... (7 Replies)
Discussion started by: anil510
7 Replies

7. Shell Programming and Scripting

NAWK conversion of hexadecimal input to decimal output via printf, I am close I can feel it

I have searched and the answers I have found thus far have led me to this point, so I feel I am just about there. I am trying to convert a column of hexadecimal to decimal values so that I can filter out via grep just the data I want. I was able to pull my original 3 character hex value and... (10 Replies)
Discussion started by: PCGameGuy
10 Replies

8. Homework & Coursework Questions

Problem with simple octal to decimal code

Hello all I started computer science and C++ programming only 6 weeks ago with no prior knowledge. It's a great language, the only other one apart from English that I hope to speak fluently quite soon. I look forward to participating further on these forums, seeking answers and looking at previous... (3 Replies)
Discussion started by: qf_woodfox
3 Replies

9. Shell Programming and Scripting

Bash Decimal Addition using bc

Hi guys/gals i have a quick question. I am trying to read from a file and add the values up but the problem is the values are not integers their floats so i tried to used bc but failed epicly lol. Any tips would be great. Thanks this is the code i have so far : while read num do ... (6 Replies)
Discussion started by: vb615
6 Replies

10. Shell Programming and Scripting

floating point not recognized by printf in bash

Dear all, I have the following question. Let's say that I have the following script #!/bin/bash value=0.4987865 a=` printf "%6.2f" $value ` b=`echo $value + $value | bc -l` echo $a echo $b exit And the exit is: 0,00 .9975730 Thus, the problem is that the printf order does not... (2 Replies)
Discussion started by: josegr
2 Replies
Login or Register to Ask a Question