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

Following is the shell script that is expected to pick one line at a time from a given file highlight it.

> myhighlight.txt
cat $file | while read -r myline
export $myline
php -r 'highlight_string("$myline");' >> myhighlight.txt

PHP is failing to understand the $myline variable. Is there any way to make this code work?

share|improve this question
up vote 1 down vote accepted

You have your quotes switched up, you want php -r "highlight_string('$myline');". As far as PHP is concerned, this will make it light up a constant string which is what bash feeds into it.

share|improve this answer

The single quotes are used when you want to literally print everything inside. So, the PHP will throw the belows error with your code:

PHP Notice:  Undefined variable: myline in Command line code on line 1

Use double quotes to expand the variables:

php -r "highlight_string($myline);" >> myhighlight.txt

No need to wrap the $myline variable with single quote.

Im summary, the single quotes are stronger than the double quotes.

PS: if you are writing in vim, pay attention to the highlight syntax feature. By default, a string will be red and a variable in the violet color.

share|improve this answer

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.