12

Accidentially, I found out that wc counts differently depending on how it gets the input from bash:

$ s='hello'
$ wc -m <<<"$s"
6
$ wc -c <<<"$s"
6
$ printf '%s' "$s" | wc -m
5
$ printf '%s' "$s" | wc -c
5

Is this - IMHO confusing - behaviour documented somewhere? What does wc count here - is this an assumed newline?

rexkogitans
  • 1,319
  • 11
  • 17

2 Answers2

38

The difference is caused by a newline added to the here string. See the Bash manual:

The result is supplied as a single string, with a newline appended, to the command on its standard input (or file descriptor n if n is specified).

wc is counting in the same way, but its input is different.

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
  • 7
    If should be noted that to print the (arbitrary) content of a variable without an added newline character, it should be `printf %s "$var"` (or `print -rn -- "$var"` with ksh-like shells), not `printf "$var"` which wouldn't work correctly for values of `$var` that contain `%` or backslash characters (or start with `-` with most implementations). – Stéphane Chazelas Jan 30 '18 at 16:54
  • Note that the original here-string implementation in the Unix port of `rc` did not add that newline character. – Stéphane Chazelas Jan 30 '18 at 16:56
26

It's a succeeding newline added by the here-string redirector:

$ s="hello"
$ hexdump -C <<<"$s"
00000000  68 65 6c 6c 6f 0a                                 |hello.|
00000006
$ printf "$s" | hexdump -C
00000000  68 65 6c 6c 6f                                    |hello|
00000005
ilkkachu
  • 133,243
  • 15
  • 236
  • 397
Murphy
  • 2,609
  • 1
  • 13
  • 21