0

I observed different results when I tried echoing value and counting the number of characters with "wc -c" on Linux and SunOS(Unix).

root@SunOS-machine:~# echo "1" | wc -c
       2
root@Linux-machine:/# echo "1" | wc -c
2

Looks like there are leading spaces added in Unix machines.

root@SunOS-machine:~# echo "1" | wc -c | wc -c
       9
root@Linux-machine:/# echo "1" | wc -c | wc -c
2

I am updating the scripts to remove the leading spaces to handle in Unix systems.

My query is to understand this behavior on Unix systems. Is this by default on Unix systems?

3

According to http://pubs.opengroup.org/onlinepubs/9699919799/:

By default, the standard output shall contain an entry for each input file of the form:

"%d %d %d %s\n", <newlines>, <words>, <bytes>, <file>

If the -m option is specified, the number of characters shall replace the <bytes> field in this format.

If any options are specified and the -l option is not specified, the number of <newline> characters shall not be written.

If any options are specified and the -w option is not specified, the number of words shall not be written.

If any options are specified and neither -c nor -m is specified, the number of bytes or characters shall not be written.

If no input file operands are specified, no name shall be written and no <blank> characters preceding the pathname shall be written.

If more than one input file operand is specified, an additional line shall be written, of the same format as the other lines, except that the word total (in the POSIX locale) shall be written instead of a pathname and the total of each column shall be written as appropriate. Such an additional line, if any, is written at the end of the output.

So it would seem that neither implementation is strictly conforming, as the standard seems to dictate that there should be 2 leading blanks. Standards are a funny thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.