Shell – How to produce a file encoded in Mac OS Roman

character encodingshell

Inside a bash script I have a line that creates and then inside a loop, append strings to a text file, something like

echo $myTextString >> file.txt 

I need this final file to be formatted using Western (Mac OS Roman).

The file does not exist when the script starts, so the first appending line will create the file. Is there a way to make the file be that format or convert it at the end to this Mac OS Roman format? How do I do that inside the bash script?

Best Answer

You could use the iconv character set conversion GNU utility. Here's an example:

iconv --from-code=UTF-8 --to-code=MAC INPUTFILE > OUTPUTFILE

where INPUTFILEand OUTPUTFILE are the names of your original and converted file (which may be the same). You'd alaso need to substitute UTF-8 by your native encoding, if it's not Unicode, and MAC may not be exactly the encoding you're looking for: iconv -l gives you a list of possible encodings to choose from (in my system none of them corresponds in an obvious way to "Western (Mac OS Roman)".

Related Question