Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

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

SOLVED
Go to solution
Highlighted
CAS_2
Valued Contributor

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.

11 REPLIES
Peter Godron
Honored Contributor

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

Hi,
N="000034"
N2=`expr $N + 0`
echo $N2
34
Peter Godron
Honored Contributor

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

spex
Honored Contributor

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
James R. Ferguson
Acclaimed Contributor

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

Hi:

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

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

Regards!

...JRF...
Frank de Vries
Respected Contributor

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
CAS_2
Valued Contributor

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


James R. Ferguson
Acclaimed Contributor

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...
spex
Honored Contributor

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
Peter Nikitka
Honored Contributor
Solution

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"
Arturo Galbiati
Esteemed Contributor

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
CAS_2
Valued Contributor

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

Thanks, Peter