The following examples show that a newline is added to a here-string.
Why is this done?
xxd -p <<<'a'
# output: 610a
xxd -p <<<'a
'
# output: 610a0a
bashtext processing
The following examples show that a newline is added to a here-string.
Why is this done?
xxd -p <<<'a'
# output: 610a
xxd -p <<<'a
'
# output: 610a0a
Best Answer
The easy answer is because ksh is written that way (and bash is compatible). But there's a reason for that design choice.
Most commands expect text input. In the unix world, a text file consists of a sequence of lines, each ending in a newline. So in most cases a final newline is required. An especially common case is to grab the output of a command with a command susbtitution, process it in some way, then pass it to another command. The command substitution strips final newlines;
<<<
puts one back.Bash and ksh can't manipulate binary data anyway (it can't cope with null characters), so it's not surprising that their facilities are geared towards text data.
The
<<<
here-string syntax is mostly only for convenience anyway, like<<
here-documents. If you need to not add a final newline, useecho -n
(in bash) orprintf
and a pipeline.