1753947 Members
7838 Online
108811 Solutions
New Discussion юеВ

addition

 
SOLVED
Go to solution
SAM_24
Frequent Advisor

addition

Hi,

I am adding up like this
one=01
adding + 05 using
let one=$one+05. I get output 6 which is correct. I want to get it as 06 instead of 6.
Any help would be appreciated.

Thanks.
Never quit
3 REPLIES 3
RAC_1
Honored Contributor
Solution

Re: addition

printf "%.2d\n" ${one}

Anil
There is no substitute to HARDWORK
Mark Grant
Honored Contributor

Re: addition

Well you might output it using "printf" and a format or you could do

[[ $RESULT -le 10 ]] && $RESULT="0$RESULT"

probably better ways though
Never preceed any demonstration with anything more predictive than "watch this"
A. Clay Stephenson
Acclaimed Contributor

Re: addition

The typeset shell command will do what you want:

#!/usr/bin/sh

typeset -i10 one=01
((one += 05))
echo "One = ${one}"
# Now let's apply a typeset
typeset -Z2 one
echo "Now one = ${one}"

NOTE: You could also apply a typeset for a new variable used only for display:
typeset -i10 one=01
((one += 05))
echo "One = ${one}"
# Now let's apply a typeset
typeset -Z2 newone=${one}
echo "Newone = ${newone}"

If it ain't broke, I can fix that.