Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SC2003 expr as octal to decimal converter #1910

Closed
ChillerDragon opened this issue Apr 7, 2020 · 2 comments
Closed

SC2003 expr as octal to decimal converter #1910

ChillerDragon opened this issue Apr 7, 2020 · 2 comments

Comments

@ChillerDragon
Copy link

I try to increment numbers prefixed with zeros. I used expr to convert them to decimal. Because bash thinks they are octal:

$ x="0008"
$ echo "$((x + 1))"
-bash: 0008: value too great for base (error token is "0008")

$ echo "$(expr "$x" + 1)" # <- works but throws SC2003
9
@ChillerDragon
Copy link
Author

Okay I solved it using:

$ shopt -s extglob
$ x="0008"
$ x="${x##+(0)}"
$ echo "$((x + 1))"
9

Maybe it is worth mentioning it at https://github.com/koalaman/shellcheck/wiki/SC2003

@mnuccioarpae
Copy link

mnuccioarpae commented Oct 14, 2021

As an alternative to removing the leading zeroes, you can specify the base for the number:

$ x=0008
$ echo $((10#$x + 1))
9

I have documented it on the wiki: https://github.com/koalaman/shellcheck/wiki/SC2003

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants