Timeline for String to integer in Shell
Current License: CC BY-SA 4.0
18 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Mar 14, 2019 at 15:45 | comment | added | Jay jargot | @StéphaneChazelas: thx for the final version | |
| Mar 14, 2019 at 10:50 | history | edited | Stéphane Chazelas | CC BY-SA 4.0 |
need LC_ALL=C for [0-9] to match only decimal digits, need to get only the first matching line, otherwise the variable could conrtain newline characters. The header needs to be at the start of the line. Shells like bash treat numbers wiith leading 0 as octal, we want at least one digit.
|
| Mar 14, 2019 at 10:31 | comment | added | Stéphane Chazelas |
@WeijunZhou, yes sorry my bad 9+9/9 is 10 not 1, (9+9)/9 is 2.
|
|
| Mar 14, 2019 at 10:12 | comment | added | Jay jargot | @WeijunZhou: you were right about the parameter expansion, the answer had been edited again. | |
| Mar 14, 2019 at 10:09 | comment | added | Jay jargot | @StéphaneChazelas: true! answer Edited again | |
| Mar 14, 2019 at 10:09 | history | edited | Jay jargot | CC BY-SA 4.0 |
added 52 characters in body
|
| Mar 14, 2019 at 10:06 | comment | added | Weijun Zhou |
@StéphaneChazelas $contentlength/9 gives 10 in bash.
|
|
| Mar 14, 2019 at 10:05 | comment | added | Jay jargot |
@StéphaneChazelas: ha ok, I never used the $(( )) with variables containing something else than a number: string 9+9 for example.
|
|
| Mar 14, 2019 at 10:01 | comment | added | Stéphane Chazelas |
@Jayjargot, there will be arithmetic evaluation with both syntax in ksh, bash and zsh (hence the command injection vulnerabilities when the input is not sanitized), one difference is when the value is like 9+9. Then contentlength/9 will give 2 and $contentlength/9 will give 1.
|
|
| Mar 14, 2019 at 10:00 | comment | added | Jay jargot | The answer had been edited. | |
| Mar 14, 2019 at 10:00 | comment | added | Weijun Zhou |
But what if parameter expansion is needed? $ in $((${b/5/7}+1)) is needed.
|
|
| Mar 14, 2019 at 10:00 | history | edited | Jay jargot | CC BY-SA 4.0 |
added 57 characters in body
|
| Mar 14, 2019 at 9:58 | comment | added | Jay jargot |
There will be ARITHMETIC EVALUATION with this syntax, you can read this in the man bash: Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax.. In other words: do not use $ for variable with this syntax.
|
|
| Mar 14, 2019 at 9:57 | comment | added | Weijun Zhou |
I won't call $(($contentlength/9)) incorrect, just that the $ is unnecessary, but there are cases where this $ is needed.
|
|
| Mar 14, 2019 at 9:51 | vote | accept | CommunityBot | ||
| Mar 14, 2019 at 9:51 | comment | added | user341722 |
It worked but what's the difference between echo $((contentlength/9)) and echo $(($contentlength))?
|
|
| Mar 14, 2019 at 9:49 | vote | accept | CommunityBot | ||
| Mar 14, 2019 at 9:50 | |||||
| Mar 14, 2019 at 9:46 | history | answered | Jay jargot | CC BY-SA 4.0 |