Bug 249522 - wc(1) prints leading spaces before the count, which appears to not be POSIX-compliant
Summary: wc(1) prints leading spaces before the count, which appears to not be POSIX-c...
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Only Me
Assignee: freebsd-bugs (Nobody)
URL:
Keywords: standards
Depends on:
Blocks:
 
Reported: 2020-09-22 11:01 UTC by Mateusz Piotrowski
Modified: 2020-09-23 16:08 UTC (History)
2 users (show)

See Also:


Attachments
Patch reformatting output / Usage update (876 bytes, text/plain)
2020-09-23 03:01 UTC, Daniel Dowse
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Mateusz Piotrowski freebsd_committer 2020-09-22 11:01:25 UTC
This document[1] suggests that there should be no leading whitespace characters printed before the count. Also, this is how GNU wc behaves, e.g.:

```
$ echo 123 | wc -c
4
```

The FreeBSD wc(1) works like this, however:

```
$ echo 123 | wc -c
       4
```

Our behaviour tends to be the source of bugs when people try to do things like this:

```
if [ "$(wc -l file)" -gt 10 ]; then ...
```

[1]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/wc.html
Comment 1 Conrad Meyer freebsd_committer 2020-09-22 16:27:05 UTC
Where is the bug?

$ [ "   4" -gt 10 ]; echo $?
1
$ [ "4" -gt 10 ]; echo $?
1
$ [ 4 -gt 10 ]; echo $?
1
$ [ "$( (echo;echo;echo) | wc -l)" -gt 10 ]; echo $?
1
$ [ "$( (echo;echo;echo) | wc -l)" -gt 1 ]; echo $?
0
Comment 2 Mateusz Piotrowski freebsd_committer 2020-09-22 18:39:28 UTC
(In reply to Conrad Meyer from comment #1)
Ugh, sorry for the noise. I should have double checked the code samples.

The bug can be seen when using [[ in Bash, e.g.:

```
$ [[ $(echo 1) = 1 ]]; echo $?
0
$ [[ $(echo line | wc -l) = 1 ]]; echo $?
1
```
Comment 3 Daniel Dowse 2020-09-23 03:01:04 UTC
Created attachment 218200 [details]
Patch reformatting output / Usage update

1. The Patch will change/reformat the output of wc(1) that is done via libxo(3) and removes leading spaces. 

2. The Usage output of wc(1) will be the same as described in the manpage.

Please test. Thanks.