Sequence of Hex

Some time ago, I wrote about command line sequences on this blog. One thing I didn’t realize, until learning it today from Claude, is that seq supports hex values. This means something like this is completely valid:

for i in $(seq 0x1 0x10); do
    echo $i
done

This will print out the numbers from 1 to 16. This is pretty useful if working with unicode, for example, and are trying to, for example, see which of a range of characters are found within a file:

for i in $(seq 0x06e0 0x06ed); do
    # we want to make it in the format of \u06e9, for example
    char=$(printf "\u$(printf '%04x' $i)")
    echo "Checking $char (U+$(printf '%04x' $i))"
    rg "$char" file.txt && echo "Found match"
done

Note that you can’t use the -f flag in seq here because it does not support the %x modifier for displaying hex, which is why the separate printf is necessary. I also learned that if you know the decimal value, you don’t need to use seq directly and can just do something like:

for i in {1760..1773}; do
    char=$(printf "\u$(printf '%04x' $i)")
    # ... same as the above
done

Cool!