Append zeros before a value as per variable

Hello-

I have a variable which contains a number, I need to populate number of zeros before another value as per this variable value.

for example:

I have variable X whose content is 5, variable Y whose content is 123

Now append number of zeros as per variable X before varible 'Y' and the hole resultant has to be captured in another variable.

expected result : 00000123

Thanks
Raghavendra

Hi,

Is this what you are after?

$ X=5
$ Y=123
$ printf "%0*d" $(( $X + ${#Y} )) $Y
00000123

Another way, less shell specifuc:

#  x=5;y=123;t=$(echo  | nawk -v X=x -v Y=$y '{printf "%05s%d",$1,Y}'); echo $t
00000123

HTH

My shell solution should be working in bash, ksh, zsh, dash and probably other shells as well.

One way to do it with Perl:

$
$ x=5
$ y=123
$
$ ##
$ perl -le "print '0'x$x,$y"
00000123
$
$ # variable assignment
$ z=$(perl -e "print '0'x$x,$y")
$ echo $z
00000123
$

tyler_durden