<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div>I wrote a random password generator shell script, the core of which is this one-liner:</div><div><br></div><div>dd if=/dev/urandom bs=1 count=256 2>/dev/null | tr -dc 'A-Za-z0-9!@$%^&*(){}[]=+-_/?\|~`' | head -c 32</div><div><br></div><div>The very ugly string 'A-Za-z0-9!@$%^&*(){}[]=+-_/?\|~`' is the ALLOWED values.  The two counts are replaced by variables, the first 'count=' needing to be a lot bigger than the final '-c <number>' which is the length of the password generated.  The size difference is necessary because 'tr' throws away a lot of values.</div><div><br></div><div>I've never had a problem with this on Linux, but on a Mac under some circumstances we get:<br></div><div><br></div><div>    tr: Illegal byte sequence<br></div><div><br></div><div>My coworker, who's also using the script, always got that error.  It seems to come down to locale settings.  Mine by default are:</div><div><br></div><div>$ locale</div><div>LANG="en_CA.UTF-8"<br>LC_COLLATE="en_CA.UTF-8"<br>LC_CTYPE="en_CA.UTF-8"<br>LC_MESSAGES="en_CA.UTF-8"<br>LC_MONETARY="en_CA.UTF-8"<br>LC_NUMERIC="en_CA.UTF-8"<br>LC_TIME="en_CA.UTF-8"<br>LC_ALL=<br></div><div><br></div><div>My co-worker's settings are:</div><div><br></div><div>LANG="en_US.UTF-8"<br>LC_COLLATE="en_US.UTF-8"<br>LC_CTYPE="en_US.UTF-8"<br>LC_MESSAGES="en_US.UTF-8"<br>LC_MONETARY="en_US.UTF-8"<br>LC_NUMERIC="en_US.UTF-8"<br>LC_TIME="en_US.UTF-8"<br>LC_ALL="en_US.UTF-8"</div><div><br></div><div>A reliable fix (so far ...):</div><div><br></div><div>    $ export LC_CTYPE=C<br>    $ export LC_ALL=C</div><div>    $ dd if=/dev/urandom bs=1 count=256 2>/dev/null | tr -dc 'A-Za-z0-9!@$%^&*(){}[]=+-_/?\|~`' | head -c 32<br>    z%V;d9uZfWLTgsT*J]Bz`mAmA<br></div><div><br></div><div>I'd really like to understand what the problem is, why 'tr' barfs, and what the 'locale' settings have to do with this.  Thanks.</div><div><br></div><div>(Should anyone have arguments against this as a method of password generation, I'll entertain those too.  And yes, I'm aware of 'apg' but it's not readily available for Mac and this is much lighter weight.)<br></div><div><br>-- <br><div dir="ltr" class="gmail_signature">Giles<br><a href="https://www.gilesorr.com/" target="_blank">https://www.gilesorr.com/</a><br><a href="mailto:gilesorr@gmail.com" target="_blank">gilesorr@gmail.com</a></div></div></div></div></div></div></div></div></div></div>