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

1 Answer 1

3

Why not simply use the proper commands:

echo "<?php" && cat test.php
2
  • 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 Commented May 14, 2016 at 0:27
  • @the_velour_fog: My pleasure! :) Commented May 14, 2016 at 0:37

You must log in to answer this question.

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