Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

I'm trying to write a one-liner to convert html entities present in some files (all html with UTF-8 encoding)

I've tried recode HTML_4.0 file.htm but that also converts non-ASCII characters (it breaks the UTF-8 characters)

In StackOverflow I found something that works for one file:

php -r '$f=@fopen("file.htm", "r");echo html_entity_decode(fread($f, 20000));fclose($f);'

but when I try to make it for multiple files with

for fi in *.htm; do php -r '$f=@fopen("$fi", "r");echo html_entity_decode(fread($f, 20000));fclose($f);';done

I know the problem here is how to "escape" $fi (bash variable) so PHP doesn't read it as a PHP variable. Any advice?

share|improve this question
up vote 3 down vote accepted

You're very nearly there.

And as it happens, the question isn't about how to do it, it's actually about how to get bash to interpret variables and pass them on.

You've got:

for fi in *.htm; do php -r '$f=@fopen("$fi", "r");echo html_entity_decode(fread($f, 20000));fclose($f);';done

Which has single quotes, with double quotes inside.

Single quotes aren't interpolated inside by bash, but double quotes are.. so if you change it to:

for fi in *.htm; do php -r "\$f=@fopen(\"$fi\",'r');echo html_entity_decode(fread(\$f, 20000));fclose(\$f);"; done

Because we're now using double quotes, we also have to escape the $ signs, otherwise bash thinks they're interpolatable variables, and replaces them out.

share|improve this answer

With bash, (single) quotes ' are used to prevent parameter expansion (variables). So you should inverse single and double quotes usage in your bash call to php, and add escaped doubles quotes for the php command arg:

for fi in *.htm; do fi=\"$fi\"; php -r "\$f=@fopen($fi, 'r');echo html_entity_decode(fread(\$f, 20000));fclose(\$f);"; done

Or, simpler, based on Michael Hampton answer, just do:

for fi in *.htm; do cat $fi | php -R 'echo html_entity_decode($argn);'; done
share|improve this answer
A very good answer, It also teachs me a little more of php. Thank you for your time. – Diego Alcántara May 27 '13 at 17:22

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.