Very easy Bash scripting question

I’m trying to compare the counter in my for loop in the if statement. I want it to print the contents of the counter when it is equal to 35. Somehow I am making a syntax error, in the line with the if statement. Any help would be greatly appreciated. Also, is variable “counter” an integer, if not what is it?

Thanks!

—begin code—

#!/bin/bash

for counter in {1…40}
do
if [$counter -eq “35”]
then
echo $counter
fi
done

Try the following:

$ cat counter.sh
#!/bin/bash

# the range needs two dots, and not three
for counter in {1..40}; do
  # the condition can be enclosed in both single or double square brackets
  if [[ "${counter}" -eq "35" ]]; then
    echo "${counter}"
  fi
done

# the script works as expected
$ bash counter.sh
35
1 Like