Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keep Leading Zero 1

Status
Not open for further replies.

vodkadrinker

Technical User
May 16, 2002
163
GB
Example

>echo 08 + 01|bc -l
9

and I am after 09 is there an easy way to do this please.
 
One way:
printf "%02d" $(echo 08 + 01|bc -l)

Another way:
typeset -Z2 i=$(echo 08 + 01|bc -l)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Yet another way:
typeset -Z2 i=$(( 8 + 1 ))

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Code:
typeset -Z2 var
var=$(echo 08 + 01|bc -l)
echo $var
09
From TFM
typeset [+HLRZfilrtux [n]] [Name[= Value]] ... Sets attributes and values for shell parameters. When invoked inside a function, a new instance of the Name parameter is created. The parameter value and type are restored when the function completes. You can specify the following flags with the typeset command:

-Z
Right-justifies and fills with leading zeros if the first nonblank character is a digit and the -L flag has not been set. If the n parameter has a nonzero value, it defines the width of the field; otherwise, it is determined by the width of the value of its first assignment.

Ceci n'est pas une signature
Columb Healy
 
PHV beat me to it!

Ceci n'est pas une signature
Columb Healy
 
I like the printf option as it keeps things simple.

Many thanks everyone,

VD
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top