Search code examples
terminalubuntu-18.04pidcat

Why does only one output of the 'cat' command come with a linebreak?


Since my reputation is too low to post an image I will reproduce the terminal output where my question originated from:

username@computer:/run$ cat rsyslogd.pid 
599username@computer:/run$ cat acpid.pid 
636
username@computer:/run$

cat acpid.pid comes with a linebreak whereas cat rsyslog.pid doesn't.

But if I open both files there is no visible difference (e.g. the file acpid.pid doesn't have an additional blank line)

The question is: why does one .pid file come with a linebreak and the other one doesn't?

Additional information: my operating system is Ubuntu 18.04.3


Solution

  • The rsyslogd.pid file probably doesn't end with a newline character (ASCII 0x0A).

    You didn't mention how you opened the files, but, I suspect you used a text editor which will not display non-printable characters (like newline and backspace). Rather than using a text editor try looking at the raw file with the hexdump tool. Then compare the hex values against an ASCII table. I think you will find that the non-printable characters after the 599 and 636 are different.

    hexdump -C rsyslogd.pid
    hexdump -C acpid.pid
    

    The following sequence of commands reproduces your output. The key is to use the -n flag for the echo command to create a file without a newline character at the end.

    $ echo -n test > file_no_new_line.txt
    $ echo test > file_with_new_line.txt
    $ cat file_no_new_line.txt
    test$ cat file_with_new_line.txt
    test
    $
    

    Here is the output of hexdump for the two files shown in my example.

    $ hexdump -C file_no_new_line.txt
    00000000  74 65 73 74                                       |test|
    00000004
    $ hexdump -C file_with_new_line.txt
    00000000  74 65 73 74 0a                                    |test.|
    00000005
    $
    

    The command output, in this case from cat, and the shell prompt ($) running into each other is also shell dependent. If the behavior can't be reproduce with the steps above try another shell (e.g. /bin/sh)