Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

#!/bin/sh
> myhighlight.txt
file='to_study.php'
cat $file | while read -r myline
do
export $myline
php -r 'highlight_string("$myline");' >> myhighlight.txt
done

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

share|improve this question

2 Answers 2

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

 
discard

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.