tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: POSIX.2, IFS and echo command



On Sat, Jul 06, 2024 at 06:09:16PM +0200, tlaronde%kergis.com@localhost wrote:
> With sh(1), and specially on NetBSD, I tend to expect this behavior:
> 
> $ line=$(printf "a\tb")
> $ echo $line | od -a
> 0000000    a  sp   b  nl
> 0000004

line has the value a, tab, b.

Given the default value of IFS (space, tab, newline):
$line splits this into a, b,
so echo is as-if it were executed with "echo", "a", "b".

POSIX recommends against using echo in favour of printf due to the
nonportability of echo, but in this case this is okay
(no \es, first argument doesn't start with -).

Thus (POSIX.1-2024 line numbers):
93207  STDOUT
93208  The echo utility arguments shall be separated by single <space>
93209  characters and a <newline> character shall follow the last argument.

So echo produces a, space, b, newline.

Your sample above is (for the default IFS), guaranteed to hold.

If IFS=' ' (so tab doesn't split line) or echo "$line" then you'd expect
  $ echo "$line" | od -ta
  0000000   a  ht   b  nl
  0000004

Best,

Attachment: signature.asc
Description: PGP signature



Home | Main Index | Thread Index | Old Index