0

I am using paste command in the mail that I am sending using shell script. The output on the console looks perfect but the output in the mail is not in the same format. How do I make the format in the mail too same as in the console. The example below is just an example. The arrays values are dynamic. So I cannot use html table.

#!/bin/bash

BASETABLE[0]="ABC"
BASETABLE[1]="XYZ"
WORKFLOW[0]="123"
WORKFLOW[1]="789"

paste -d' ' <(printf "\n") <(printf "\n%-12.12s\n" "${BASETABLE[@]}") <(printf "\n%s\n" "${WORKFLOW[@]}")

echo "Test" `paste -d' ' <(printf "\n") <(printf "\n%-12.12s\n" "${BASETABLE[@]}") <(printf "\n%s\n" "${WORKFLOW[@]}")` | mail -s "Test" user@xyz.com

Outout on Console:

ABC          123

XYZ          789

Output in Mail:

ABC 123 XYZ 789
user3198755
  • 477
  • 2
  • 10
  • 21

1 Answers1

0

The problem is your use of the echo statement. echo will not preserve newlines in an unquoted string. For example, if I have a variable containing a newline:

my_variable="This is
a test"

If I echo it without quotes, newlines will be transformed into spaces:

$ echo $my_variable
This is a test

But if I quote the variable, newlines will be preserved:

$ echo "$my_variable"
This is
a test

So you can get the output you want by simply moving the closing quote in your echo statement, so that it becomes:

echo "Test `paste -d' ' <(printf "\n") <(printf "\n%-12.12s\n" "${BASETABLE[@]}") <(printf "\n%s\n" "${WORKFLOW[@]}")`"

This will output:

Test
 ABC          123

 XYZ          789

Instead of:

Test ABC 123 XYZ 789
larsks
  • 277,717
  • 41
  • 399
  • 399