0

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?

2 Answers 2

1

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.

0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .