Re: Parameter substitution in shell: does ${##} work ? (374 Views)
Reply
Valued Contributor
CAS_2
Posts: 187
Registered: ‎04-07-2005
Message 1 of 12 (374 Views)
Accepted Solution

Parameter substitution in shell: does ${##} work ?

Hi

Let be a numeric value into a shell variable:

N=000034

I'm interested in removing leading zeroes.
According to man pages, sh-posix can do this task by means of ${N##0}:

echo ${N#0} # removes only one leading zero
echo ${N##0} # removes all leading zeroes

but in my case, that doesn't work:

${N##0} works as ${N#0}, i.e, only one leading zero is removed in both cases.

Can anyone help me ?

P.D: I'd prefer this shell trick rather than using an external command.

Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 2 of 12 (374 Views)

Re: Parameter substitution in shell: does ${##} work ?

Hi,
N="000034"
N2=`expr $N + 0`
echo $N2
34
Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 3 of 12 (374 Views)

Re: Parameter substitution in shell: does ${##} work ?

Honored Contributor
spex
Posts: 1,367
Registered: ‎05-14-1996
Message 4 of 12 (374 Views)

Re: Parameter substitution in shell: does ${##} work ?

Hi,

$ N=000034
$ echo ${N##*0}
34

For more information on parameter substitution:
http://docs.hp.com/en/B2355-90046/ch19s03.html#d0e17999

PCS
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 12 (374 Views)

Re: Parameter substitution in shell: does ${##} work ?

Hi:

# N=000034
# X=${N##*0}
# echo $X

Note the splat ("*") following the "##".

Regards!

...JRF...
Respected Contributor
Frank de Vries
Posts: 733
Registered: ‎11-13-1996
Message 6 of 12 (374 Views)

Re: Parameter substitution in shell: does ${##} work ?

Hello

Indeed right your are:
use typeset -i N=000034

It seems without interger the '*' but '?'
comes to the rescue :
see examples:

[vwbsrv3:/d]# print ${N##*0}
34
[vwbsrv3:/]# print ${N##?0}
0034
[vwbsrv3:/]# print ${N##??0}
034
[vwbsrv3:/]# print ${N#??0}
034
-No difference here

[vwbsrv3:/]# print ${N##???0}
34
[vwbsrv3:/stand/build]#


But when you make it a INTEGER,

[vwbsrv3:/stand/build]# typeset -i N=00034
[vwbsrv3:/stand/build]# print ${N##*0}
34
[vwbsrv3:/stand/build]# print ${N##0}
34

It works:
Look before you leap
Valued Contributor
CAS_2
Posts: 187
Registered: ‎04-07-2005
Message 7 of 12 (374 Views)

Re: Parameter substitution in shell: does ${##} work ?

James, read these examples below:

example 1: OK

X=00000034
echo ${X##*0}
34

example 2: failed

X=50034
echo ${X##*0}
34


Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 8 of 12 (374 Views)

Re: Parameter substitution in shell: does ${##} work ?

Hi (again) Cas:

Ok, that's obvious in retrospect. We haven't nor can't anchor to the beginning of the string. I hadn't paid attention to that.

Using 'typeset' keeps things shell-bound:

# typeset -LZ N=0000034;echo ${N}
34

# typeset -LZ N=1000034;echo ${N}
1000034

Regards!

...JRF...
Honored Contributor
spex
Posts: 1,367
Registered: ‎05-14-1996
Message 9 of 12 (374 Views)

Re: Parameter substitution in shell: does ${##} work ?

Hello,

# N1=0000034
# N2=50034
# echo $N1
0000034
# echo $N2
50034
# echo $(( 10#$N1 ))
34
# echo $(( 10#$N2 ))
50034

PCS
Honored Contributor
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 10 of 12 (374 Views)

Re: Parameter substitution in shell: does ${##} work ?

Hi,

if you really cannot use a typeset because of restrictions in the value, your variables are set, this will do it:

X=00000034
print ${X#${X%%[1-9]*}}
34

X=00010034
print ${X#${X%%[1-9]*}}
10034

X=500000034
print ${X#${X%%[1-9]*}}
500000034

The trick is, to determine the number of leading zeros (if any exist) first.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Esteemed Contributor
Arturo Galbiati
Posts: 830
Registered: ‎02-10-2003
Message 11 of 12 (374 Views)

Re: Parameter substitution in shell: does ${##} work ?

Hi,

/> X=00000034
/> echo $X
00000034
/> X=$(( X+0 ))
/> echo $X

the rtick is to add 0 to force the varibale to be an integer. In this way the leading zeros will be removed.

HTH,
Art
Valued Contributor
CAS_2
Posts: 187
Registered: ‎04-07-2005
Message 12 of 12 (374 Views)

Re: Parameter substitution in shell: does ${##} work ?

Thanks, Peter
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.