Why is there a difference between the meaning of NUM in head -c and tail -c?

I will clarify what I mean with the following commands:

$ echo "words" | tail -c +1 words $ echo "words" | tail -c +2 ords $ echo "words" | head -c -1 words$ echo "words" | head -c -2 word$ 

The first command does nothing.
The second command removes the first letter.
The third command removes the newline.
The last command removes the last 2 characters including the newline.

So why does head removes 2 bytes when using -c and tail only one. This looks like some real inconsistency or is there an underlying meaning?

1 Answer

Citing man tail:

use -c +NUM to output starting with byte NUM of each file

Citing man head:

-c (…) [-]NUM
with the leading '-', print all but the last NUM bytes of each file(…)

The logic with

tail -c +1 

is to print all from byte number 1, i.e. the first byte, while with

head -c -1 

it’s to print all but the last 1 byte.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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