Why does wc <<<â$stringâ show a one-byte-longer length than printf â$stringâ | wc?
Clash Royale CLAN TAG#URR8PPP
up vote
11
down vote
favorite
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?
bash newlines wc here-string
add a comment |Â
up vote
11
down vote
favorite
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?
bash newlines wc here-string
3
You can always pipe tood -c
to see exactly what you have.
â Thorbjørn Ravn Andersen
Jan 30 at 17:19
Or, better,xxd -g1
.
â Ruslan
Jan 30 at 18:27
1
I hopeprintf "$s"
isn't your actual script... hopefully you meantprintf "%s" "$s"
â Mehrdad
Jan 30 at 23:23
Since there were so many comments about printf, I edited my post to reflect best practice.
â rexkogitans
Jan 31 at 6:01
add a comment |Â
up vote
11
down vote
favorite
up vote
11
down vote
favorite
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?
bash newlines wc here-string
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?
bash newlines wc here-string
edited Jan 31 at 5:59
asked Jan 30 at 12:26
rexkogitans
308110
308110
3
You can always pipe tood -c
to see exactly what you have.
â Thorbjørn Ravn Andersen
Jan 30 at 17:19
Or, better,xxd -g1
.
â Ruslan
Jan 30 at 18:27
1
I hopeprintf "$s"
isn't your actual script... hopefully you meantprintf "%s" "$s"
â Mehrdad
Jan 30 at 23:23
Since there were so many comments about printf, I edited my post to reflect best practice.
â rexkogitans
Jan 31 at 6:01
add a comment |Â
3
You can always pipe tood -c
to see exactly what you have.
â Thorbjørn Ravn Andersen
Jan 30 at 17:19
Or, better,xxd -g1
.
â Ruslan
Jan 30 at 18:27
1
I hopeprintf "$s"
isn't your actual script... hopefully you meantprintf "%s" "$s"
â Mehrdad
Jan 30 at 23:23
Since there were so many comments about printf, I edited my post to reflect best practice.
â rexkogitans
Jan 31 at 6:01
3
3
You can always pipe to
od -c
to see exactly what you have.â Thorbjørn Ravn Andersen
Jan 30 at 17:19
You can always pipe to
od -c
to see exactly what you have.â Thorbjørn Ravn Andersen
Jan 30 at 17:19
Or, better,
xxd -g1
.â Ruslan
Jan 30 at 18:27
Or, better,
xxd -g1
.â Ruslan
Jan 30 at 18:27
1
1
I hope
printf "$s"
isn't your actual script... hopefully you meant printf "%s" "$s"
â Mehrdad
Jan 30 at 23:23
I hope
printf "$s"
isn't your actual script... hopefully you meant printf "%s" "$s"
â Mehrdad
Jan 30 at 23:23
Since there were so many comments about printf, I edited my post to reflect best practice.
â rexkogitans
Jan 31 at 6:01
Since there were so many comments about printf, I edited my post to reflect best practice.
â rexkogitans
Jan 31 at 6:01
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
38
down vote
accepted
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.
7
If should be noted that to print the (arbitrary) content of a variable without an added newline character, it should beprintf %s "$var"
(orprint -rn -- "$var"
with ksh-like shells), notprintf "$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 at 16:54
Note that the original here-string implementation in the Unix port ofrc
did not add that newline character.
â Stéphane Chazelas
Jan 30 at 16:56
add a comment |Â
up vote
26
down vote
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
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
38
down vote
accepted
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.
7
If should be noted that to print the (arbitrary) content of a variable without an added newline character, it should beprintf %s "$var"
(orprint -rn -- "$var"
with ksh-like shells), notprintf "$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 at 16:54
Note that the original here-string implementation in the Unix port ofrc
did not add that newline character.
â Stéphane Chazelas
Jan 30 at 16:56
add a comment |Â
up vote
38
down vote
accepted
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.
7
If should be noted that to print the (arbitrary) content of a variable without an added newline character, it should beprintf %s "$var"
(orprint -rn -- "$var"
with ksh-like shells), notprintf "$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 at 16:54
Note that the original here-string implementation in the Unix port ofrc
did not add that newline character.
â Stéphane Chazelas
Jan 30 at 16:56
add a comment |Â
up vote
38
down vote
accepted
up vote
38
down vote
accepted
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.
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.
edited Jan 30 at 16:36
answered Jan 30 at 12:30
Stephen Kitt
142k22308370
142k22308370
7
If should be noted that to print the (arbitrary) content of a variable without an added newline character, it should beprintf %s "$var"
(orprint -rn -- "$var"
with ksh-like shells), notprintf "$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 at 16:54
Note that the original here-string implementation in the Unix port ofrc
did not add that newline character.
â Stéphane Chazelas
Jan 30 at 16:56
add a comment |Â
7
If should be noted that to print the (arbitrary) content of a variable without an added newline character, it should beprintf %s "$var"
(orprint -rn -- "$var"
with ksh-like shells), notprintf "$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 at 16:54
Note that the original here-string implementation in the Unix port ofrc
did not add that newline character.
â Stéphane Chazelas
Jan 30 at 16:56
7
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 at 16:54
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 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 at 16:56
Note that the original here-string implementation in the Unix port of
rc
did not add that newline character.â Stéphane Chazelas
Jan 30 at 16:56
add a comment |Â
up vote
26
down vote
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
add a comment |Â
up vote
26
down vote
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
add a comment |Â
up vote
26
down vote
up vote
26
down vote
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
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
edited Jan 30 at 12:41
ilkkachu
49.8k674137
49.8k674137
answered Jan 30 at 12:31
Murphy
1,7471517
1,7471517
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f420655%2fwhy-does-wc-string-show-a-one-byte-longer-length-than-printf-string-w%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
3
You can always pipe to
od -c
to see exactly what you have.â Thorbjørn Ravn Andersen
Jan 30 at 17:19
Or, better,
xxd -g1
.â Ruslan
Jan 30 at 18:27
1
I hope
printf "$s"
isn't your actual script... hopefully you meantprintf "%s" "$s"
â Mehrdad
Jan 30 at 23:23
Since there were so many comments about printf, I edited my post to reflect best practice.
â rexkogitans
Jan 31 at 6:01