0

I have this lines that I will insert in a mail called "header".

header="
-------------------------------------
Name\tAge\tGender\tAddress
-------------------------------------
"

Unfortunately, the \t does not work as I expected it to be. I even tried to put the \t between these two ``.

Then I put this $header in the auto-email.

mailx -s "$subject" -r "$from" "$to" <<-eof
$header

$body

$footer
eof
}
  • What do you want to achieve? doing echo -e $header works fine with me – fredtantini Jun 16 '15 at 8:27
  • just want to have proper alignments of columns using the tabs, i thought its just as easy as above but it doesnt work on mine – cube00 Jun 16 '15 at 8:32
  • Updated my answer to include the mailx part – Lambert Jun 16 '15 at 9:33
0

Use the \n to add newlines to your header:

header="-------------------------------------\nName\tAge\tGender\tAddress\n-------------------------------------\n"

Then use for instance echo -e $header to display:

echo -e $header
-------------------------------------
Name    Age Gender  Address
-------------------------------------

If echo -e does not work, use printf instead. If your output starts with dashes call printf -- "$header" to tell printf to interpret the complete variable as output instead of trying to parse them as arguments.

After more testing the original variable also works with printf:

header="
-------------------------------------
Name\tAge\tGender\tAddress
-------------------------------------
"

printf -- "$header"

-------------------------------------
Name    Age     Gender  Address
-------------------------------------

To use this in mailx you can use:

mailx -s "$subject" -r "$from" "$to" <<eof
$header
$body
$footer
eof
  • echo -e does not work on mine. it will just echo -e as well – cube00 Jun 16 '15 at 8:38
  • Which OS/Distro are you using? – Lambert Jun 16 '15 at 8:44
  • my OS is AIX, shell is ksh – cube00 Jun 16 '15 at 8:50
  • Can you try printf -- "-------------------------------------\nName\tAge\tGender\tAddress\n-------------------------------------\n" – Lambert Jun 16 '15 at 8:52
  • The reason is that echo -e is non-standard, while in ksh printf will also work, as it is standard, and ksh (ksh93 on AIX) supports also ANSI strings. . – Janis Jun 16 '15 at 8:57
2

If you use ANSI strings:

header=$'
-------------------------------------
Name\tAge\tGender\tAddress
-------------------------------------
'

you have the correct translation of any ANSI escaped characters already in your variable. (Note: the newline characters could also be defined as \n, but it wouldn't add to legibility in your case, so I'd keep your original format as also depicted in this answer).

There's also the possibility to let other tools do the conversion at every place where it is used (while the string still contains the textual representation); examples for that are: Using echo -e (if your version of echo supports that option), or using the variable as format string in printf where escapes will also be interpreted, as in: printf "$header".

Discussion: Using printf has the advantage to be POSIX standard. Using ANSI strings has the advantage that the conversion is consistently present in the variable, and needs no further interpretation.

Since (as the updated question shows) the variable will be used in context of a program (mailx) that will not do interpretation of escaped characters the variable needs to already contain the appropriate formatting; so using the ANSI Strings (with ksh93 on AIX) seems to be the appropriate solution. For a standard conforming solution you could also re-assign the variable using printf, e.g.: header="..." ; header=$( printf "$header" )

  • Janis, can you please explain printf vs ANSI a bit more? If I read man printf I find the following: "The format operands support the full range of ANSI C/C99/XPG6 formatting specifiers as well as additional specifiers" – Lambert Jun 16 '15 at 9:06
  • @Lambert; Sure, please just tell me what sort of explanation do you need? - Since you already quoted the printf manpage I suppose that part is clear. - The ANSI Strings is a ksh feature that is also available in other shells (like bash or zsh); what it does is simply an interpretation of (ANSI-) escaped characters (similar to what is done in the printf format string), but since it's done with the definition of the string you need just one interpretation, and on every place where you use the variable no further interpretation/conversion of escaped characters is necessary. – Janis Jun 16 '15 at 9:22
  • @\Janis, thanks, after reading it over and with your last comment it is more clear to me know. (strange behaviour, if I use @Janis, it is replaced with nothing) – Lambert Jun 16 '15 at 9:25
  • @Lambert; I noticed that "@"-issue as well with some account names; the notification reached me nonetheless. - BTW: I now consistently use a semicolon instead of a comma after the account name, and that fixed the issue for me. – Janis Jun 16 '15 at 10:12
0

Another possible option is to use awk:

$ echo "Name Age Gender Address" | awk -F' ' '{ printf "%s\t%s\t%s\t%s\n", $1, $2, $3, $4 }'

produces:

Name    Age     Gender  Address

Or using printf directly:

$ printf "%s\t%s\t%s\t%s\t\n" Name Age Gender Address

produces:

Name    Age     Gender  Address
0

From man echo

Normally you could distinguish between a flag and a string that begins with a hyphen by using a (double hyphen). Since no flags are supported with the echo command, a (double hyphen) is treated literally.

And in the 2nd example:

Note: You must put the message in quotation marks if it contains escape sequences. Otherwise, the shell interprets the \ (backslash) as a metacharacter and treats the \ differently.

And indeed, on AIX:

~> header="
-------------------------------------
Name\tAge\tGender\tAddress
-------------------------------------
"

~> echo -e $header
-e ------------------------------------- Name   Age     Gender  Address -------------------------------------
~> echo "$header"

-------------------------------------
Name    Age     Gender  Address
-------------------------------------

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.