Search code examples
bash

Pipe string with newline to command in Bash


I am trying to pass in a string containing a newline to a PHP script via Bash.

#!/bin/bash

REPOS="$1"
REV="$2"

message=$(svnlook log $REPOS -r $REV)
changed=$(svnlook changed $REPOS -r $REV)

/usr/bin/php -q /home/chad/www/mantis.localhost/scripts/checkin.php <<< "${message}\n${changed}"

When I do this, I see the literal "\n" rather than the escaped newline:

blah blah issue 0000002.\nU app/controllers/application_controller.rb

How can I translate '\n' to a literal newline?

By the way: What does <<< do in Bash? I know < passes in a file...


Solution

  • Try

    echo -e "${message}\n${changed}" | /usr/bin/php -q /home/chad/www/mantis.localhost/scripts/checkin.php
    

    Where -e enables interpretation of backslash escapes (according to man echo).

    Note that this will also interpret backslash escapes which you potentially have in ${message} and in ${changed}.


    From the Bash manual:

    Here Strings

    A variant of here documents, the format is:

    <<<word
    

    The word is expanded and supplied to the command on its standard input.

    So I'd say

    the_cmd <<< word
    

    is equivalent to

    echo word | the_cmd