Well, @uint128_t caught the error.
Your script appears to attempt to print out all the IP addresses within 192.168.0.0/16, rather than all the class C networks with that prefix, so I'll assume that's your code is a better description of the result you're looking for.
And I'll submit the following as a "better use of bash":
#!/usr/bin/env bash
# Declare an array
ip=(192 168 -1 -1)
# Step through the third quad in the array
while [ $((ip[2]++)) -lt 255 ]; do
# Step through the fourth quad in the array
while [ $((ip[3]++)) -lt 255 ]; do
# Print the array, with dots as a field separator
IFS=. eval echo '"${ip[*]}"'
done
# Reset the last quad once we're done with this subnet
ip[3]=-1
done
There will be those who say that eval is evil, but it's perfectly safe in this context, where input data are known and you're protecting things with single quotes.
This solution avoids extra counters, and perhaps gives you flexibility to do other things with your IPs if you desire.
I should mention another subtlety, which is [ $((ip[2]++)) -lt 255 ]. This increments the array element, but because the ++ is AFTER the variable, the value that is used for comparison (-le) is the one before the increment occurs. So we stop the loop when the compared number was less than 255, because that means the last run of the loop will occur when the variable incremented one higher, to 255. If for some reason you wanted to compare the value after the increment instead of before, you could prepend the variable with ++ instead of appending it: $((++ip[2])).
Another fun approach might be to capitalize on the fact that IP addresses are simply numbers, and that dotted quads are a translation of that number:
#!/usr/bin/env bash
# Set your start and maximum IPs as integers
ip=$(( 192*2**24 + 168*2**16 ))
max=$(( ip + 255*2**8 + 255 ))
# Convert each integer into dotted quad notation
while [ $ip -le $max ]; do
echo $(( ip/2**24 )).$(( ip/2**16 %256 )).$(( ip/2**8 % 256 )).$(( ip % 256 ))
((ip++))
done
$is not required inside the double-parentheses construct. E.g.$((count1 + 1))works perfectly.#!/bin/sh, it implies that they will be portable. An answer below suggested using{0..255}. While BASH will expand this, other POSIX /bin/sh implementations likely will not. If you're writing for bash, it's best to use#!/usr/bin/env bashas your shebang. If you're writing for portability, best not to use bashisms.whileloop answers both resetcount2after the innerwhilecompletes. I'd set it before it starts (and not before the outer loop starts). But both techniques achieve the same result.