if the variable contains only digits, and doesn't overflow, then
#!/usr/bin/ksh
typeset -i x
y="00123"
eval x=$y
echo $y $x
Doesn't work, it interprets y as octal.
With bash, you can do
$ var=00003701; echo $((10#$var))
3701
$ shopt -s extglob; var=00004402; echo ${var##+(0)}
4402
for bash:
shopt -s extglob
x=000876
y=${x##+(0)}
printf "%s\n" $x $y
--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous
Also ksh...
>
> $ var=00003701; echo $((10#$var))
> 3701
$ var=00003701; echo $((10#$var))
3701
>
> $ shopt -s extglob; var=00004402; echo ${var##+(0)}
> 4402
$ var=00004402; echo ${var##+(0)}
4402
But mind that whether the result is desired depends on the
requirements...
$ var=0000; echo $((10#$var)) # zero
0
$ var=0000; echo ${var##+(0)} # empty
Janis
Another BASH solution:
a=0000gh
[[ $a =~ '^0*(.*)' ]]
a="${BASH_REMATCH[1]}"
$ $SHELL --version
GNU bash, version 3.2.39(20)-release (i686-pc-cygwin)
Copyright (C) 2007 Free Software Foundation, Inc.
$ a=0000gh
$ [[ $a =~ '^0*(.*)' ]]
$ a="${BASH_REMATCH[1]}"
$ echo "<$a>"
<>
$
$ a=0000gh
$ a=${a##+(0)}
$ echo "<$a>"
<gh>
Why are you proposing solutions to your own questions, though?
Ed.
In bash
a="00099"
printf "%d\n" $a
Did you try that one?
$ var=0077
$ printf "%d\n" $var
63
$ var=00099
$ printf "%d\n" $var
bash: printf: 00099: invalid number
0
With bash, you can do
$ var=00003701; echo $((10#$var))
With bash, you can do
So the above version has a bug?
> $ a=0000gh
> $ a=${a##+(0)}
> $ echo "<$a>"
> <gh>
After you do shopt -s extglob I presume.
> Why are you proposing solutions to your own questions, though?
As opposed to what , keeping it to myself ?
With bash, you can do
$ var=00003701; echo $((10#$var))
3701
$ shopt -s extglob; var=00004402; echo ${var##+(0)}
4402
--
sh -c 'a123=$4;b=$2;j=4464346045754551;h=$1;r=4165722075063752;s=$3;
while [ "$r" != "" ];do x=0;y=;w=${#h};while [ $x -lt $(($w-1)) ];do
y=$y?;x=$((x+1));done;o=$o$s${h%$y}${j%$y}${r%$y};r=${r#?};j=${j#?};
h=${h#?};done;eval "$a123 $b$o$b"' sh 1111111111101110 \' '\' printf
> $ var=00003701; echo $((10#$var))
> 3701
Thanks. I didn't recognize the above syntax, and had to dig a bit to
figure out what it is.
For the benefit of any others reading this, from info bash, the $(( indicates
arithmetic expansion and the 10# indicates that what follows the # is a base
10 number.
Regards, Dave Hodgins
--
Change nomail.afraid.org to ody.ca to reply by email.
(nomail.afraid.org has been set up specifically for
use in usenet. Feel free to use it yourself.)
I don't know. Does it or does the proposed solution only work with
some version(s) of bash?
> > $ a=0000gh
> > $ a=${a##+(0)}
> > $ echo "<$a>"
> > <gh>
>
> After you do shopt -s extglob I presume.
>
> > Why are you proposing solutions to your own questions, though?
>
> As opposed to what , keeping it to myself ?
As opposed to either:
a) not asking the question, or
b) when asking the question stating you have a specific solution
already but you aren't happy with it for reason XXXX and you're
looking for a different solution that's faster or more concise or
whatever so we don't waste time recreating it or proposing solutions
that don't have the characteristics you want.
Ed.
I'm sure it wasn't in BASH from the beginning so it only works
on some versions. But I tested on version 3.1.17 which is
earlier than yours so something else is going on. After some
googling I found E14 in
http://www.faqs.org/faqs/unix-faq/shell/bash There is the
compat31 shopt option which restores the earlier behaviour:
http://ftp.gnu.org/gnu/bash/bash-3.2-patches/bash32-039
Even after reading E14 it's not clear to me why the BASH people
decided to change the behaviour and I'm sure it made life
difficult for a lot of people. Based on what I read the
following will work on both my version and yours:
regexp='^0*(.*)'
a=0000gh
[[ $a =~ $regexp ]]
a="${BASH_REMATCH[1]}"
> > > Why are you proposing solutions to your own questions, though?
>
> > As opposed to what , keeping it to myself ?
>
> As opposed to either:
>
> a) not asking the question, or
> b) when asking the question stating you have a specific solution
> already but you aren't happy with it for reason XXXX and you're
> looking for a different solution that's faster or more concise or
> whatever so we don't waste time recreating it or proposing solutions
> that don't have the characteristics you want.
The answer is obviously that I only thought of my solution after I
made the OP.
A standard solution:
case $var in
(*[!0]*)
tmp=${var%%[!0]*}
var=${var#"$tmp"};;
("") ;;
(*) var=0
esac
--
Stéphane
And of course as usual, what ksh or bash can do, so can zsh.
[...]
>> $ shopt -s extglob; var=00004402; echo ${var##+(0)}
>> 4402
>
> $ var=00004402; echo ${var##+(0)}
> 4402
You want setopt kshglob for zsh to recognise those extended ksh
globbing patterns. Or use zsh extended patterns:
setopt extendedglob
echo ${var##0#}
>
> But mind that whether the result is desired depends on the
> requirements...
>
> $ var=0000; echo $((10#$var)) # zero
> 0
$ var=1+2; echo $((10#$var))'
3
$ var=010+020; echo $((10#$var))'
26
With zsh:
setopt extendedglob
echo ${var/(#m)<->/$((MATCH))}
>
> $ var=0000; echo ${var##+(0)} # empty
Now try that with any shell other than zsh and this value of
$var:
var=000/*/*/*/../../../*/*/*/../../../*/*/*
(not on a production server though!)
Variables should be quoted.
--
Stéphane
This takes first price ! But why not
case $var in
*[!0]*)
tmp=${var%%[!0]*}
var=${var#"$tmp"};;
"") ;;
*) var=0
esac
What is the role of the opening parenthesis?
Try what exactly? Your standard method on BASH works
with the above value of var. Which variables should
be quoted?
legibility, better integration with text editors. Having matched
parenthesis avoid some syntax problems with some shells in some
circumstances.
--
Stéphane
For example ?
$ pdksh -c 'echo $(case a in *) echo $a;; esac)'
pdksh: syntax error: `;;' unexpected
mksh, posh, zsh, bash are also affected.
ash, ksh93 are not. bash 4 will not be.
--
Stéphane