Register
It is currently Sat Aug 30, 2014 6:21 am

Stripping Leading Zeroes


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Mon May 25, 2009 4:42 pm   

Joined: Mon May 25, 2009 4:32 pm
Posts: 2
I have a little script where I need to strip leading zeroes from a variable and while I can make it work on the command line, it won't work in a script. Here is basically what I am doing:

Code:
myvar=001
myvar=${myvar##+(0)}
echo myvar

When I enter this on the command line (separated by ';') it prints '1', but if I put it in a little file, make that executable and run it, it prints '001'. What am I doing wrong?

This is on Kubuntu Jaunty amd64.


Top
 Profile  
 PostPosted: Mon Jun 01, 2009 5:58 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
Seems odd. I don't have the version you're using so I can't test properly but this seems to be a bug/whatever in bash v2. In 3.2.48 it works as you expect.

You may be able to get around it with something like;

Code:
myvar=$(echo 001 | sed -r 's/0+//')

<):)


Top
 Profile  
 PostPosted: Mon Jun 01, 2009 7:06 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
DarthWavy wrote:
Seems odd. I don't have the version you're using so I can't test properly but this seems to be a bug/whatever in bash v2. In 3.2.48 it works as you expect.

You may be able to get around it with something like;

Code:
myvar=$(echo 001 | sed -r 's/0+//')

<):)


bad method... if the number is 100 then you'll end up with 1 instead.
use 's/^0+//' instead, this way you ensure that only the zero's that is at the start of the line is removed.

Best regards
Fredrik eriksson


Top
 Profile  
 PostPosted: Tue Jun 02, 2009 2:37 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
of course you're right Fredrik :)


Top
 Profile  
 PostPosted: Tue Jun 16, 2009 11:50 pm   

Joined: Mon May 25, 2009 4:32 pm
Posts: 2
DarthWavy wrote:
Seems odd. I don't have the version you're using so I can't test properly but this seems to be a bug/whatever in bash v2. In 3.2.48 it works as you expect.


$ bash -version
GNU bash, version 3.2.48(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.

So it can't be the bash version, must be something in Jaunty. I'll ask around there.

Thanks


Top
 Profile  
 PostPosted: Fri Jun 19, 2009 6:37 pm   
User avatar

Joined: Sat Jun 13, 2009 8:53 pm
Posts: 73
Location: Texas!
Make sure you are using the #! /bin/bash header and not just sh. I think Ubuntu uses dash instead of bash by default. Maybe that could be a problem?


Top
 Profile  
 PostPosted: Sun Jun 21, 2009 2:48 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 535
I have the same behaviour, as exposed by the OP : works in tty but not from a script :(

using : Debian GNU/Linux

- Lenny
GNU bash, version 3.2.39

- Squeeze
GNU bash, version 3.2.48


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP