| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
var=2 n=3
var+=$nIn bash/ksh, use an (( arithmetic context ))
(( var += n ))or declare the variable as an integer type:
declare -i var=2
n=4
var+=$nFor POSIX sh, use an $((arithmetic expansion)):
var=$((var+n))The problematic code attempts to add 2 and 3 to get 5.
Instead, += on a string variable will concatenate, so the result is 23.
If you do want to concatenate a number, for example to append trailing zeroes, you can silence the warning by quoting the number:
var+="000"| Back | FazBrowse Home | New Git URL |