Interview Prep/Shell Scripting/Troubleshooting: tools, portability, permissions and environment

On macOS, `tr '[:lower:]' '[:upper:]' < export.csv` fails with `tr: Illegal byte sequence`, and `sed` gives `sed: RE error: illegal byte sequence`.

IntermediateTroubleshootingTroubleshooting: tools, portability, permissions and environment
Answer

Likely causes → The file is not valid UTF-8 (often Latin-1 or Windows-1252 exported from a spreadsheet), and the locale is UTF-8, so the tools refuse bytes that are not valid characters.

Diagnose →

file -I export.csv                         # charset=iso-8859-1
iconv -f UTF-8 -t UTF-8 export.csv > /dev/null   # reports the first invalid byte position

Fix → Convert the encoding, or process as raw bytes:

iconv -f WINDOWS-1252 -t UTF-8 export.csv > export.utf8.csv
LC_ALL=C tr '[:lower:]' '[:upper:]' &lt; export.csv

Prevent → Normalise encoding at ingestion (iconv once), and set LC_ALL=C in scripts that treat data as bytes.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Troubleshooting: tools, portability, permissions and environment