0

I have a script a.sh build up an here document which looks like below

read -r -d "" message << EOF
line1
line2
line3
line4

EOF

then I want to pass this message (which contain four lines) to another program so I did

b.sh $message

however in b.sh I did

reMess=$1
cat $reMess

it only shows line1 not the rest 3 lines, could anyone explain what should I do to achieve this?

Second questions I changed the code in a.sh which make the message store in a file then read it from b.sh, could anyone suggest in b.sh how could I read this file in a here document please. code detail as below

a.sh
read -r -d "" message << EOF
line1
line2
line3
line4

EOF
echo "$message" > /tmp/message.txt

b.sh
read -r -d "" information << EOF
inforation1
$(cat /tmp/message.txt)
EOF
echo "$information"

it return me nothing any suggestion where is wrong? Thank you for your help

Linsong Guo
  • 37
  • 1
  • 1
  • 6
  • 1
    It should be `b.sh "$message"`. Check this: http://stackoverflow.com/a/1655389/171318 – hek2mgl Feb 26 '15 at 00:17
  • There's nothing obvious wrong with your second example. Did you perhaps misspell `"$information"` in the original? – rici Feb 26 '15 at 02:55

1 Answers1

0

The only problem I see with your code is that it is needlessly complex.

message='line1
line2
line3
line4'

information="information1
$message"  # need double quotes rather than single to interpolate $message

If you don't like the embedded newlines, Bash has C-string syntax.

message=$'line1\nline2\nline3\nline4'

But there really isn't any benefit to this, and it hampers portability to other shells. You'll get used to multi-line strings quickly.

tripleee
  • 175,061
  • 34
  • 275
  • 318