1

If I have a file called test.php and its contents are

$a = "apple";    
echo PHP_EOL;
echo "  the value of \$a is $a" . PHP_EOL;

I want to cat the contents of test.php to STDOUT but I want to add a header string <?php to the beginning of the output. My desired outcome is to run a command like

% cat ..... test.php

desired output in terminal

<?php
$a = "apple";    
echo PHP_EOL;
echo "  the value of \$a is $a" . PHP_EOL;

I have tried assigning the string <?php to a variable and concatenating the variable with a here-string, e.g.

% php='<?php'       
% cat <<<$php
<?php
% cat <<<$php test.php 
$a = "apple";
echo PHP_EOL;
echo "  the value of \$a is $a" . PHP_EOL;

When I try to concatenate a here-string to a file the here-string is ignored.
How can I insert a header into the STDOUT before the cat'ed file?

  • 3
    cat - test.php <<<$php? – muru May 14 '16 at 0:20
  • @muru its seems like the command is backwards, but that works! – the_velour_fog May 14 '16 at 0:28
  • it's not backwards. - tells cat to print stdin input, which in this case should come before the filename. – muru May 14 '16 at 0:30
3

Why not simply use the proper commands:

echo "<?php" && cat test.php
  • 1
    the reason I didn't use this form is because - once I got this command working - I then wanted to attach the STDOUT to a pipe. But I just tried your command wrapped in command group brackets and it works with a pipe, e.g. { echo "<?php" && cat test.php; } | php thanks – the_velour_fog May 14 '16 at 0:27
  • @the_velour_fog: My pleasure! :) – Julie Pelletier May 14 '16 at 0:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.