I have a function vercomp
that compares two version strings and determines which one is greater. When I add this alias:
alias grep='grep -EI --colour=always'
to a seemingly unrelated part of my bashrc
file I get this error:
-bash: 10#24 > 10#24: syntax error: invalid arithmetic operator (error token is "24 > 10#24")
-bash: 10#24 < 10#24: syntax error: invalid arithmetic operator (error token is "24 < 10#24")
Note that the error is emitted twice, I'm assuming because the error is processed twice (i.e. this isn't a typo on my part). Note that when I remove the alias, everything works fine. Why is this error being generated and how do I mitigate it?
The lines that are of interest, below, are probably the ones marked:
if ((10#${ver1[i]} > 10#${ver2[i]})); then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]})); then
return 2
fi
EDIT: Adding more context
I'm using GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin11)
on Mac OS X 10.7.5 (Lion). I'm calling vercomp
like this:
if [[ $OS = 'Mac' ]]; then
### EMACS VERSION CHECK
# make sure that we're working with emacs >= 24
wanted_ver=24
curr_ver=`emacs --version | grep -oE '[[:digit:]]+\.[.[:digit:]]*'`
echo $curr_ver
vercomp $curr_ver $wanted_ver
Note that I'm calling grep
to initialize curr_ver
. I still can't figure out why the error is happening though, but using grep -EI --colour
doesn't generate the error, so that answers the second part of my question. Does anyone know why the error happens?
vercomp () {
## returns: 0 equal
## 1 ver1 > ver 2
## 2 ver1 < ver 2
if [[ $1 == $2 ]]; then
return 0
fi
# IFS (Internal Field Separator) Fields are separated by a '.'
# ($var) notation means turn $var into an array according to the IFS.
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
# ${#var[@]} = the number of elements in the array/var.
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)); do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++)); do
if [[ -z ${ver2[i]} ]]; then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
# <num>#$var converts the value of $var to the base of <num>
if ((10#${ver1[i]} > 10#${ver2[i]})); then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]})); then
return 2
fi
done
return 0
}