2

I need to display a bash script on a webpage without any formatting.

The bash script uses standard 'here document' blocks. When I try to output the script using the PHP heredoc function, it cuts off the output when it encounters a '<<' sub-string. I thought the PHP heredoc function did not require escaping.

How do I output this script correctly?

<?php

$string = $_GET["string"];

$bashscript = <<<MYMARKER
<pre>

#!/bin/sh
rm /tmp/blue.sh
cat <<INSTALL > /tmp/blue.sh
#!/bin/sh
cd /tmp
mkdir output
cd output
cat <<EOF > interface.conf
remote $string
EOF
INSTALL

</pre>
MYMARKER;

echo $bashscript;

?>

The output I get on the page is

#!/bin/sh
rm /tmp/blue.sh
cat < /tmp/blue.sh
#!/bin/sh
cd /tmp
mkdir output
cd output
cat < interface.conf
remote 
EOF
INSTALL

1 Answer 1

3

That's because <INSTALL > and <EOF > are interpreted as tags in your browser (unrecognized though). Open it with right click -> view source and you'll see it right. Just move out the <pre> and use htmlspecialchars() to display it correctly:

$bashscript = <<<MYMARKER
... everything without the <pre> tags ...
MYMARKER;

echo '<pre>'.htmlspecialchars($bashscript).'</pre>';
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I should have picked that up, time for a break!
Happens to us all; glad I could help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.