Sending Mail in Bash Script Outputs Literal \N Instead of a New Line

Sending mail in bash script outputs literal \n instead of a new line

In bash you should use following syntax

message_txt=$'This is just a test.\n Goodbye!'

Single quotes preceded by a $ is a new syntax that allows to insert escape sequences in strings.

Check following documentation about the quotation mechanism of bash for ANSI C-like escape sequences

mail bash variable on a new line

It seems that Outlook removed extra line breaks from my message. I was able to use awk to fix the problem.

echo "$d" | awk '{ print $0"   " }' | mail -s "subject" -r "Name<Name@somewhere.com >" first.last@somewhere.com

This reads every single line, and re-prints it with three spaces at the end. Outlook is now happy.
https://blog.dhampir.no/content/outlook-removes-extra-line-breaks-from-plain-text-emails-how-to-stop-it

Sendmail bash script, concatenate strings with new line

You're sending an HTML email and need to use <br> instead of \n.

You need to enclose the whole string in double quotes. Try body="$xmsg <br> $links"

How to insert new line in the email using linux mail command?

Try using echo -e

echo -e "Hello \n World"

You can type man echo from the command line to read more.

How to insert new line in message body of email using Bash?

Tested on MacOS with Bash 3.2

mail -s "$subject" MyTestEmail@gmail.com <<< $(printf "%s\r\n%s\n" "Note that $start and $end use server time"  "the end")

This is a screen shot from gmail of the email received

Screen Shot from Gmail - Email Received

How can I 'echo' out things without a newline?

Yes. Use the -n option:

echo -n "$x"

From help echo:

-n do not append a newline

This would strips off the last newline too, so if you want you can add a final newline after the loop:

for ...; do ...; done; echo

Note:

This is not portable among various implementations of echo builtin/external executable. The portable way would be to use printf instead:

printf '%s' "$x"

How can I have a newline in a string in sh?

If you're using Bash, you can use backslash-escapes inside of a specially-quoted $'string'. For example, adding \n:

STR=$'Hello\nWorld'
echo "$STR" # quotes are required here!

Prints:

Hello
World

If you're using pretty much any other shell, just insert the newline as-is in the string:

STR='Hello
World'

Bash recognizes a number of other backslash escape sequences in the $'' string. Here is an excerpt from the Bash manual page:

Words of the form $'string' are treated specially. The word expands to
string, with backslash-escaped characters replaced as specified by the
ANSI C standard. Backslash escape sequences, if present, are decoded
as follows:
\a alert (bell)
\b backspace
\e
\E an escape character
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\\ backslash
\' single quote
\" double quote
\nnn the eight-bit character whose value is the octal value
nnn (one to three digits)
\xHH the eight-bit character whose value is the hexadecimal
value HH (one or two hex digits)
\cx a control-x character

The expanded result is single-quoted, as if the dollar sign had not
been present.

A double-quoted string preceded by a dollar sign ($"string") will cause
the string to be translated according to the current locale. If the
current locale is C or POSIX, the dollar sign is ignored. If the
string is translated and replaced, the replacement is double-quoted.

Printf example in bash does not create a newline

The backtick operator removes trailing new lines. See 3.4.5. Command substitution at http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html

Note on edited question

Compare:

[alvaro@localhost ~]$ printf "\n"

[alvaro@localhost ~]$ echo "\n"
\n
[alvaro@localhost ~]$ echo -e "\n"

[alvaro@localhost ~]$

The echo command doesn't treat \n as a newline unless you tell him to do so:

NAME
echo - display a line of text
[...]
-e enable interpretation of backslash escapes

POSIX 7 specifies this behaviour here:

[...] with the standard output of the command, removing sequences of one or more characters at the end of the substitution

bash output with extra newline within the string

You get an extra newline because for doesn't just split on newlines -- it also splits on spaces, so instead of getting a pid/fd pair assigned to PidFd, it first has a PID assigned, and then has a FD. As extensively documented at DontReadLinesWithFor, the best-practice approach is to use a while read loop instead.

Moreover, one doesn't need to use awk, grep, or sed at all for what you're trying to accomplish here (although grep can help performance on systems with a large number of sockets, at the expense of making it worse on systems with only a small number of sockets):

#!/usr/bin/env bash
case $BASH_VERSION in '') echo "ERROR: Your shell must be bash, not sh" >&2; exit 1;; esac

pid_re='pid=([[:digit:]]+)($|[^[:digit:]])'
fd_re='fd=([[:digit:]]+)($|[^[:digit:]])'
while IFS= read -r line; do pid=; fd=
[[ $line =~ $pid_re ]] && pid=${BASH_REMATCH[1]}
[[ $line =~ $fd_re ]] && fd=${BASH_REMATCH[1]}
[[ $pid && $fd ]] && echo "$pid $fd"
done < <(sudo ss -p | grep CLOSE-WAIT)


Related Topics



Leave a reply



Submit