- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Parameter substitution in shell: does ${##} work ?
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2006 10:57 PM
10-23-2006 10:57 PM
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.
Solved! Go to Solution.
- Tags:
- variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2006 11:09 PM
10-23-2006 11:09 PM
Re: Parameter substitution in shell: does ${##} work ?
N="000034"
N2=`expr $N + 0`
echo $N2
34
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2006 11:13 PM
10-23-2006 11:13 PM
Re: Parameter substitution in shell: does ${##} work ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2006 11:43 PM
10-23-2006 11:43 PM
Re: Parameter substitution in shell: does ${##} work ?
$ N=000034
$ echo ${N##*0}
34
For more information on parameter substitution:
http://docs.hp.com/en/B2355-90046/ch19s03.html#d0e17999
PCS
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2006 11:50 PM
10-23-2006 11:50 PM
Re: Parameter substitution in shell: does ${##} work ?
# N=000034
# X=${N##*0}
# echo $X
Note the splat ("*") following the "##".
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2006 12:01 AM
10-24-2006 12:01 AM
Re: Parameter substitution in shell: does ${##} work ?
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:
- Tags:
- typeset
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2006 12:31 AM
10-24-2006 12:31 AM
Re: Parameter substitution in shell: does ${##} work ?
example 1: OK
X=00000034
echo ${X##*0}
34
example 2: failed
X=50034
echo ${X##*0}
34
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2006 12:50 AM
10-24-2006 12:50 AM
Re: Parameter substitution in shell: does ${##} work ?
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2006 01:04 AM
10-24-2006 01:04 AM
Re: Parameter substitution in shell: does ${##} work ?
# N1=0000034
# N2=50034
# echo $N1
0000034
# echo $N2
50034
# echo $(( 10#$N1 ))
34
# echo $(( 10#$N2 ))
50034
PCS
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2006 01:38 AM
10-24-2006 01:38 AM
Solutionif 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2006 08:03 PM
10-24-2006 08:03 PM
Re: Parameter substitution in shell: does ${##} work ?
/> 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2006 08:25 PM
10-24-2006 08:25 PM