#!/bin/bash
function thousands {
sed -re ' :restart ; s/([0-9])([0-9]{3})($|[^0-9])/\1,\2\3/ ; t restart '
}
echo 12 | thousands
echo 1234 | thousands
echo 123456 | thousands
echo 1234567 | thousands
echo 123456789 | thousands
echo 1234567890 | thousands
IyEvYmluL2Jhc2gKCmZ1bmN0aW9uIHRob3VzYW5kcyB7CiAgICBzZWQgLXJlICcgOnJlc3RhcnQgOyBzLyhbMC05XSkoWzAtOV17M30pKCR8W14wLTldKS9cMSxcMlwzLyA7IHQgcmVzdGFydCAnCn0gICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCmVjaG8gMTIgfCB0aG91c2FuZHMKZWNobyAxMjM0IHwgdGhvdXNhbmRzCmVjaG8gMTIzNDU2IHwgdGhvdXNhbmRzCmVjaG8gMTIzNDU2NyB8IHRob3VzYW5kcwplY2hvIDEyMzQ1Njc4OSB8IHRob3VzYW5kcwplY2hvIDEyMzQ1Njc4OTAgfCB0aG91c2FuZHM=
12
1,234
123,456
1,234,567
123,456,789
1,234,567,890