Reputation: 2271
How to convert all numbers in Bash/shell?
VAR=00005
VAR=00010
VAR=00601
VAR=00550
to
echo $VAR #5
echo $VAR #10
echo $VAR #601
echo $VAR #550
Upvotes: 3
Views: 7324
Reputation: 3660
echo $((10#${VAR}))
can do what you want
Works at least for bash and mksh.
From the bash manual:
Constants with a leading 0 are interpreted as octal numbers. A leading ‘0x’ or ‘0X’ denotes hexadecimal. Otherwise, numbers take the form [base#]n, where the optional base is a decimal number between 2 and 64 representing the arithmetic base, and n is a number in that base. If base# is omitted, then base 10 is used.
The docs for mksh are more concise regarding the leading zeros:
Prefixing with "10#" forces interpretation as decimal, even with leading zeros.
Upvotes: 2
Reputation: 652
The reason people are having issues with 08
and 09
is because numbers with trailing zeros are treated by the shell as octals. You can do something like this:
VAR="08"
let "VAR=10#${VAR}"
echo $VAR
to remove the trailing leading zeros.
Upvotes: 3
Reputation: 195029
or like this:
kent$ echo "00005
00010
00601
00550"|awk '$0*=1'
5
10
601
550
for your updated question (with VAR)
first of all, you should have different variable names, not all same as VAR.
see the example below:
kent$ VAR=00601
kent$ VAR=$((VAR+0))
kent$ echo $VAR
601
EDIT
for the comment.(08, 09 didn't work):
08, 09 worked here, might be something with my shell to do. I have zsh. I tested followings under bash, they worked. hope helps:
under zsh:
kent$ v=08
kent$ v=$((v+0))
kent$ echo $v
8
under bash, below worked
kent@7PLaptop:/tmp$ bash -version
GNU bash, version 3.2.48(1)-release (i486-pc-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.
kent@7PLaptop:/tmp$ v=08
kent@7PLaptop:/tmp$ v=$(sed 's/^0*//'<<< $v)
kent@7PLaptop:/tmp$ echo $v
8
Upvotes: 6
Reputation: 2271
to: ДМИТРИЙ МАЛИКОВ
printf "%d\n" 0000
printf "%d\n" 0001
printf "%d\n" 0002
printf "%d\n" 0003
printf "%d\n" 0007
printf "%d\n" 0008
printf "%d\n" 0009
printf "%d\n" 0010
printf "%d\n" 0011
printf "%d\n" 0012
result
0
1
2
3
7
0 line 8: printf: 0008: invalid octal number
0 line 9: printf: 0009: invalid octal number
8 !error - correctly 10
9 !error - correctly 11
10 !error - correctly 12
Upvotes: -2
Reputation: 241748
With extglob
, you do not need any external process:
shopt -s extglob # Enable extended globbing
for i in 00005 00010 00601 00550; do
echo ${i##+(0)}
done
Upvotes: 2
Reputation: 21972
$> cat text
00005
00010
00601
00550
$> sed -r 's/0*([0-9]*)/\1/' text
5
10
601
550
Using printf
:
$> while read n; do printf "%0d\n" $((10#$n)); done < text
5
10
601
550
Note, when a numerical format expects a number, the internal printf-command will use the common Bash arithmetic rules regarding the base. In order to force decimal representation and as a side effect also remove any leading zeros for a Bash variable we should use $((10#$n))
Upvotes: 6
Reputation: 77
You can use printf
to add or remove leading zeros:
$ printf "%05d\n" 5
00005
$ printf "%d\n" 00005
5
$ printf "%010d\n" 00005
0000000005
Upvotes: -2