How to increment the value of a (decimal) variable (with leading zero) by +1?Addition with 'sed'Increment number in bash variable stringIncrement Daily Config ValueIncrement a variable using a Bash scriptincrement a value pulled from a fileUsing awk to divide the number in each line of a file by the maximum value in that fileHow to propagate the value of a variableread a file from Server path - bashString to integer in Shell
Christmas party at employers home
Why do these two ways of understanding constant acceleration give different results?
How to treat unhandled exceptions? (Terminate the application vs. Keep it alive)
How to deal with people whose priority is to not get blamed?
Mapping string into integers
Where does the upgrade to macOS Catalina move root "/" directory files?
Demod as a neologism
Why is Trump releasing or not of his taxes such a big deal?
Are there 99 percentiles, or 100 percentiles? And are they groups of numbers, or dividers or pointers to individual numbers?
What is the gold linker?
Can Chandra, Acolyte of Flame cast Heartfire without the additional cost?
Why is my paper "under review" if it contains no results?
How can I learn to write better questions to test for conceptual understanding?
What's the meaning of java.util.@Nullable?
Why does Principal Vagina say, "no relation" after introducing himself?
Encountering former, abusive advisor at a conference
Why are second inversion triads considered less consonant than first inversion triads?
Is the tap water in France safe to drink?
Uniform Roe algebra of virtually abelian group is type I C*-algebra?
How to balance combat for a duet campaign with non-frontliner classes?
Can a Pokemon that I tried to capture from field research run away?
Can we not simply connect a battery to a RAM to prevent data loss during power cuts?
Does any politician honestly want a No Deal Brexit?
On notice period - coworker I need to train is giving me the silent treatment
How to increment the value of a (decimal) variable (with leading zero) by +1?
Addition with 'sed'Increment number in bash variable stringIncrement Daily Config ValueIncrement a variable using a Bash scriptincrement a value pulled from a fileUsing awk to divide the number in each line of a file by the maximum value in that fileHow to propagate the value of a variableread a file from Server path - bashString to integer in Shell
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
I have a file in the name of Build.number with the content value 012 which I need to increment by +1.
So, I tried this
BN=$($cat Build.number)
BN=$(($BN+1))
echo $BN >Build.number
but here I am getting the value 11 when I am expecting 013.
Can anyone help me?
bash shell-script numeric-data arithmetic
New contributor
user11668570 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
|
show 2 more comments
I have a file in the name of Build.number with the content value 012 which I need to increment by +1.
So, I tried this
BN=$($cat Build.number)
BN=$(($BN+1))
echo $BN >Build.number
but here I am getting the value 11 when I am expecting 013.
Can anyone help me?
bash shell-script numeric-data arithmetic
New contributor
user11668570 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
There are two good answers, both not only explaining the cause of the problem, but providing additional information that might be useful. Stephen's provides a reference and Stéphane's mentions other shells and provides a more efficient way of reading the data without using thecatprogram. The ideal answer would combine both. (The ideal answer would also use"%.3dn"format rather than the old leading 0 trick, which is ancient style, and misleading because in that case the leading 0 doesn't mean octal.)
– Ray Butterworth
8 hours ago
Related: unix.stackexchange.com/a/36959/117549
– Jeff Schaller♦
8 hours ago
1
@Ray why is.3dbetter than03d? Apart from the octal confusion...
– Stephen Kitt
8 hours ago
1
@StephenKitt. For d, i, o, u, x, X, a, A, e, E, f, F, g, and G conversion specifiers, leading zeros (following any indication of sign or base) are used to pad to the field width rather than performing space padding, except when converting an infinity or NaN. If the 0 and - flags both appear, the 0 flag is ignored. For d, i, o, u, x, and X conversion specifiers, if a precision is specified, the 0 flag shall be ignored. [CX] [Option Start] If the 0 and <apostrophe> flags both appear, the grouping characters are inserted before zero padding. For other conversions, the behavior is undefined.
– Ray Butterworth
5 hours ago
2
@StephenKitt, the.3dmakes the intent much more obvious: An optional precision that gives the minimum number of digits to appear for the d, i, o, u, x, and X conversion specifiers. The03dsyntax dates back to the original 1970s version, and preserving backward compatibility has made its rules extremely complicated.
– Ray Butterworth
5 hours ago
|
show 2 more comments
I have a file in the name of Build.number with the content value 012 which I need to increment by +1.
So, I tried this
BN=$($cat Build.number)
BN=$(($BN+1))
echo $BN >Build.number
but here I am getting the value 11 when I am expecting 013.
Can anyone help me?
bash shell-script numeric-data arithmetic
New contributor
user11668570 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I have a file in the name of Build.number with the content value 012 which I need to increment by +1.
So, I tried this
BN=$($cat Build.number)
BN=$(($BN+1))
echo $BN >Build.number
but here I am getting the value 11 when I am expecting 013.
Can anyone help me?
bash shell-script numeric-data arithmetic
bash shell-script numeric-data arithmetic
New contributor
user11668570 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
user11668570 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 4 hours ago
Cbhihe
4821 gold badge6 silver badges20 bronze badges
4821 gold badge6 silver badges20 bronze badges
New contributor
user11668570 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 9 hours ago
user11668570user11668570
191 bronze badge
191 bronze badge
New contributor
user11668570 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
user11668570 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
There are two good answers, both not only explaining the cause of the problem, but providing additional information that might be useful. Stephen's provides a reference and Stéphane's mentions other shells and provides a more efficient way of reading the data without using thecatprogram. The ideal answer would combine both. (The ideal answer would also use"%.3dn"format rather than the old leading 0 trick, which is ancient style, and misleading because in that case the leading 0 doesn't mean octal.)
– Ray Butterworth
8 hours ago
Related: unix.stackexchange.com/a/36959/117549
– Jeff Schaller♦
8 hours ago
1
@Ray why is.3dbetter than03d? Apart from the octal confusion...
– Stephen Kitt
8 hours ago
1
@StephenKitt. For d, i, o, u, x, X, a, A, e, E, f, F, g, and G conversion specifiers, leading zeros (following any indication of sign or base) are used to pad to the field width rather than performing space padding, except when converting an infinity or NaN. If the 0 and - flags both appear, the 0 flag is ignored. For d, i, o, u, x, and X conversion specifiers, if a precision is specified, the 0 flag shall be ignored. [CX] [Option Start] If the 0 and <apostrophe> flags both appear, the grouping characters are inserted before zero padding. For other conversions, the behavior is undefined.
– Ray Butterworth
5 hours ago
2
@StephenKitt, the.3dmakes the intent much more obvious: An optional precision that gives the minimum number of digits to appear for the d, i, o, u, x, and X conversion specifiers. The03dsyntax dates back to the original 1970s version, and preserving backward compatibility has made its rules extremely complicated.
– Ray Butterworth
5 hours ago
|
show 2 more comments
There are two good answers, both not only explaining the cause of the problem, but providing additional information that might be useful. Stephen's provides a reference and Stéphane's mentions other shells and provides a more efficient way of reading the data without using thecatprogram. The ideal answer would combine both. (The ideal answer would also use"%.3dn"format rather than the old leading 0 trick, which is ancient style, and misleading because in that case the leading 0 doesn't mean octal.)
– Ray Butterworth
8 hours ago
Related: unix.stackexchange.com/a/36959/117549
– Jeff Schaller♦
8 hours ago
1
@Ray why is.3dbetter than03d? Apart from the octal confusion...
– Stephen Kitt
8 hours ago
1
@StephenKitt. For d, i, o, u, x, X, a, A, e, E, f, F, g, and G conversion specifiers, leading zeros (following any indication of sign or base) are used to pad to the field width rather than performing space padding, except when converting an infinity or NaN. If the 0 and - flags both appear, the 0 flag is ignored. For d, i, o, u, x, and X conversion specifiers, if a precision is specified, the 0 flag shall be ignored. [CX] [Option Start] If the 0 and <apostrophe> flags both appear, the grouping characters are inserted before zero padding. For other conversions, the behavior is undefined.
– Ray Butterworth
5 hours ago
2
@StephenKitt, the.3dmakes the intent much more obvious: An optional precision that gives the minimum number of digits to appear for the d, i, o, u, x, and X conversion specifiers. The03dsyntax dates back to the original 1970s version, and preserving backward compatibility has made its rules extremely complicated.
– Ray Butterworth
5 hours ago
There are two good answers, both not only explaining the cause of the problem, but providing additional information that might be useful. Stephen's provides a reference and Stéphane's mentions other shells and provides a more efficient way of reading the data without using the
cat program. The ideal answer would combine both. (The ideal answer would also use "%.3dn" format rather than the old leading 0 trick, which is ancient style, and misleading because in that case the leading 0 doesn't mean octal.)– Ray Butterworth
8 hours ago
There are two good answers, both not only explaining the cause of the problem, but providing additional information that might be useful. Stephen's provides a reference and Stéphane's mentions other shells and provides a more efficient way of reading the data without using the
cat program. The ideal answer would combine both. (The ideal answer would also use "%.3dn" format rather than the old leading 0 trick, which is ancient style, and misleading because in that case the leading 0 doesn't mean octal.)– Ray Butterworth
8 hours ago
Related: unix.stackexchange.com/a/36959/117549
– Jeff Schaller♦
8 hours ago
Related: unix.stackexchange.com/a/36959/117549
– Jeff Schaller♦
8 hours ago
1
1
@Ray why is
.3d better than 03d? Apart from the octal confusion...– Stephen Kitt
8 hours ago
@Ray why is
.3d better than 03d? Apart from the octal confusion...– Stephen Kitt
8 hours ago
1
1
@StephenKitt. For d, i, o, u, x, X, a, A, e, E, f, F, g, and G conversion specifiers, leading zeros (following any indication of sign or base) are used to pad to the field width rather than performing space padding, except when converting an infinity or NaN. If the 0 and - flags both appear, the 0 flag is ignored. For d, i, o, u, x, and X conversion specifiers, if a precision is specified, the 0 flag shall be ignored. [CX] [Option Start] If the 0 and <apostrophe> flags both appear, the grouping characters are inserted before zero padding. For other conversions, the behavior is undefined.
– Ray Butterworth
5 hours ago
@StephenKitt. For d, i, o, u, x, X, a, A, e, E, f, F, g, and G conversion specifiers, leading zeros (following any indication of sign or base) are used to pad to the field width rather than performing space padding, except when converting an infinity or NaN. If the 0 and - flags both appear, the 0 flag is ignored. For d, i, o, u, x, and X conversion specifiers, if a precision is specified, the 0 flag shall be ignored. [CX] [Option Start] If the 0 and <apostrophe> flags both appear, the grouping characters are inserted before zero padding. For other conversions, the behavior is undefined.
– Ray Butterworth
5 hours ago
2
2
@StephenKitt, the
.3d makes the intent much more obvious: An optional precision that gives the minimum number of digits to appear for the d, i, o, u, x, and X conversion specifiers. The 03d syntax dates back to the original 1970s version, and preserving backward compatibility has made its rules extremely complicated.– Ray Butterworth
5 hours ago
@StephenKitt, the
.3d makes the intent much more obvious: An optional precision that gives the minimum number of digits to appear for the d, i, o, u, x, and X conversion specifiers. The 03d syntax dates back to the original 1970s version, and preserving backward compatibility has made its rules extremely complicated.– Ray Butterworth
5 hours ago
|
show 2 more comments
2 Answers
2
active
oldest
votes
The leading 0 causes Bash to interpret the value as an octal value; 012 octal is 10 decimal, so you get 11.
To force the use of decimal, add 10#:
BN=10#$(cat Build.number)
echo $((++BN)) > Build.number
To print the number using at least three digits, use printf:
printf "%.3dn" $((++BN)) > Build.number
add a comment
|
bash treats constants that start with 0 as octal numbers in its arithmetic expressions, so 011 is actually 9.
That's actually a POSIX requirement.
Some other shells like mksh or zsh ignore it (unless in POSIX compliant mode) as it gets in the way far more often than it is useful.
With ksh93, BN=011; echo "$(($BN))" outputs 9, but echo "$((BN))" outputs 11.
In bash, you can use BN=$((10#$(<Build.number))), which should work as long as the number doesn't start with - or +.
add a comment
|
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
user11668570 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f545909%2fhow-to-increment-the-value-of-a-decimal-variable-with-leading-zero-by-1%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The leading 0 causes Bash to interpret the value as an octal value; 012 octal is 10 decimal, so you get 11.
To force the use of decimal, add 10#:
BN=10#$(cat Build.number)
echo $((++BN)) > Build.number
To print the number using at least three digits, use printf:
printf "%.3dn" $((++BN)) > Build.number
add a comment
|
The leading 0 causes Bash to interpret the value as an octal value; 012 octal is 10 decimal, so you get 11.
To force the use of decimal, add 10#:
BN=10#$(cat Build.number)
echo $((++BN)) > Build.number
To print the number using at least three digits, use printf:
printf "%.3dn" $((++BN)) > Build.number
add a comment
|
The leading 0 causes Bash to interpret the value as an octal value; 012 octal is 10 decimal, so you get 11.
To force the use of decimal, add 10#:
BN=10#$(cat Build.number)
echo $((++BN)) > Build.number
To print the number using at least three digits, use printf:
printf "%.3dn" $((++BN)) > Build.number
The leading 0 causes Bash to interpret the value as an octal value; 012 octal is 10 decimal, so you get 11.
To force the use of decimal, add 10#:
BN=10#$(cat Build.number)
echo $((++BN)) > Build.number
To print the number using at least three digits, use printf:
printf "%.3dn" $((++BN)) > Build.number
edited 53 mins ago
answered 9 hours ago
Stephen KittStephen Kitt
208k27 gold badges493 silver badges559 bronze badges
208k27 gold badges493 silver badges559 bronze badges
add a comment
|
add a comment
|
bash treats constants that start with 0 as octal numbers in its arithmetic expressions, so 011 is actually 9.
That's actually a POSIX requirement.
Some other shells like mksh or zsh ignore it (unless in POSIX compliant mode) as it gets in the way far more often than it is useful.
With ksh93, BN=011; echo "$(($BN))" outputs 9, but echo "$((BN))" outputs 11.
In bash, you can use BN=$((10#$(<Build.number))), which should work as long as the number doesn't start with - or +.
add a comment
|
bash treats constants that start with 0 as octal numbers in its arithmetic expressions, so 011 is actually 9.
That's actually a POSIX requirement.
Some other shells like mksh or zsh ignore it (unless in POSIX compliant mode) as it gets in the way far more often than it is useful.
With ksh93, BN=011; echo "$(($BN))" outputs 9, but echo "$((BN))" outputs 11.
In bash, you can use BN=$((10#$(<Build.number))), which should work as long as the number doesn't start with - or +.
add a comment
|
bash treats constants that start with 0 as octal numbers in its arithmetic expressions, so 011 is actually 9.
That's actually a POSIX requirement.
Some other shells like mksh or zsh ignore it (unless in POSIX compliant mode) as it gets in the way far more often than it is useful.
With ksh93, BN=011; echo "$(($BN))" outputs 9, but echo "$((BN))" outputs 11.
In bash, you can use BN=$((10#$(<Build.number))), which should work as long as the number doesn't start with - or +.
bash treats constants that start with 0 as octal numbers in its arithmetic expressions, so 011 is actually 9.
That's actually a POSIX requirement.
Some other shells like mksh or zsh ignore it (unless in POSIX compliant mode) as it gets in the way far more often than it is useful.
With ksh93, BN=011; echo "$(($BN))" outputs 9, but echo "$((BN))" outputs 11.
In bash, you can use BN=$((10#$(<Build.number))), which should work as long as the number doesn't start with - or +.
answered 9 hours ago
Stéphane ChazelasStéphane Chazelas
337k58 gold badges661 silver badges1038 bronze badges
337k58 gold badges661 silver badges1038 bronze badges
add a comment
|
add a comment
|
user11668570 is a new contributor. Be nice, and check out our Code of Conduct.
user11668570 is a new contributor. Be nice, and check out our Code of Conduct.
user11668570 is a new contributor. Be nice, and check out our Code of Conduct.
user11668570 is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f545909%2fhow-to-increment-the-value-of-a-decimal-variable-with-leading-zero-by-1%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
There are two good answers, both not only explaining the cause of the problem, but providing additional information that might be useful. Stephen's provides a reference and Stéphane's mentions other shells and provides a more efficient way of reading the data without using the
catprogram. The ideal answer would combine both. (The ideal answer would also use"%.3dn"format rather than the old leading 0 trick, which is ancient style, and misleading because in that case the leading 0 doesn't mean octal.)– Ray Butterworth
8 hours ago
Related: unix.stackexchange.com/a/36959/117549
– Jeff Schaller♦
8 hours ago
1
@Ray why is
.3dbetter than03d? Apart from the octal confusion...– Stephen Kitt
8 hours ago
1
@StephenKitt. For d, i, o, u, x, X, a, A, e, E, f, F, g, and G conversion specifiers, leading zeros (following any indication of sign or base) are used to pad to the field width rather than performing space padding, except when converting an infinity or NaN. If the 0 and - flags both appear, the 0 flag is ignored. For d, i, o, u, x, and X conversion specifiers, if a precision is specified, the 0 flag shall be ignored. [CX] [Option Start] If the 0 and <apostrophe> flags both appear, the grouping characters are inserted before zero padding. For other conversions, the behavior is undefined.
– Ray Butterworth
5 hours ago
2
@StephenKitt, the
.3dmakes the intent much more obvious: An optional precision that gives the minimum number of digits to appear for the d, i, o, u, x, and X conversion specifiers. The03dsyntax dates back to the original 1970s version, and preserving backward compatibility has made its rules extremely complicated.– Ray Butterworth
5 hours ago