Integer comparison in bash Integer comparison in bash shell shell

Integer comparison in bash


The command [ can't handle arithmetics inside its test. Change it to:

if [ $i -ne $((hosts_count-1)) ]; then

Edit: what @cebewee wrote is also true; you must put a space in front of the closing ]. But, just doing that will result in yet another error: extra argument '-'


  1. The ] must be a separate argument to [.
  2. You're assuming you can do math in [.

    if [ $i -ne $(($hosts_count - 1)) ] ; then


In bash, you can avoid both [ ] and [[ ]] by using (( )) for purely arithmetic conditions:

if (( i != hosts_count - 1 )); then  cmd="$cmd"fi