回复PHP到文件linux [重复]

This question already has an answer here:

I'm trying to programatically create a PHP file:

<?php
if (!empty($_GET['code'])
{
  // Do 1
}
else
{
  // Do else
}
?>

I've got this:

sudo echo '<?php
if (!empty($_GET['code']))
{
}
else
{
}
?>' | sudo tee -a /var/www/html/index.php

But that's stripping out the single quotes from around the code ... I've tried all sorts such as double quotes and escaping the single quotes.

I'm sure this is simple! Help! :)

</div>

This is how single quotes should be escaped in bash and pretty much any shell:

echo '<?php
if (!empty($_GET['\'code\'']))
{
} else {
}
?>' | tee /var/www/html/index.php

See also: https://github.com/koalaman/shellcheck/wiki/SC1003