Pipe string with newline to command in bash?

Viewed 34090

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

Any ideas how to translate '\n' to a literal newline?

By the way: what does <<< do in bash? I know < passes in a file...

4 Answers

It is better to use here-document syntax:

cat <<EOF
copy $VAR1 $VAR2
del $VAR1
EOF

You can use magical Bash $'\n' with here-word:

cat <<< "copy $VAR1 $VAR2"$'\n'"del $VAR1"

or pipe with echo:

{ echo copy $VAR1 $VAR2; echo del $VAR1; } | cat

or with printf:

printf "copy %s %s\ndel %s" "$VAR1" "$VAR2" "$VAR1" | cat

Test it:

env VAR1=1 VAR2=2 printf "copy %s %s\ndel %s" "$VAR1" "$VAR2" "$VAR1" | cat
Related