Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
My iconv -l do not list no similar encoding. – enzotib Dec 2 '11 at 19:24
up vote 6 down vote accepted

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)".

share|improve this answer
wow, thanks! That's it! – SpaceDog Dec 3 '11 at 7:20

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.