Why is the number of lines in my bash output showing as 4 instead of 3?

  • Thread starter Thread starter r0bHadz
  • Start date Start date
Click For Summary
SUMMARY

The discussion centers on a discrepancy in the line count of bash command output using the command "ps -ly" and "ps -ly | wc -l". The user observed that while the output visually displays three lines, the line count command returns four. The confusion was resolved when the user confirmed that the additional line counted by "wc -l" was due to a trailing newline character in the output. This highlights the importance of understanding how command-line tools interpret output formatting.

PREREQUISITES
  • Basic knowledge of bash commands
  • Understanding of the "ps" command and its options
  • Familiarity with the "wc" command for word and line counting
  • Experience with text editors like Vim for output inspection
NEXT STEPS
  • Explore advanced options of the "ps" command for process listing
  • Learn about handling newline characters in bash scripting
  • Investigate the "awk" command for more complex text processing
  • Study the differences between various output formats in bash commands
USEFUL FOR

System administrators, bash script developers, and anyone troubleshooting command-line output discrepancies.

r0bHadz
Messages
194
Reaction score
17
TL;DR
Using command ps -ly I get output of three lines
piping that output to wc -l it says that there are four lines. Why
In bash, I am writing the following:

ps - ly

this command gives me output of three lines

then I do:

ps -ly | wc -l

to read the number of lines. It says 4.

I clearly see that there are three lines. Thinking that there might be a new line possibly, I open export the command output to a file and open it in vim and no, there are just three lines.

I am wondering why the number is 4?
 
Computer science news on Phys.org
hmm wrong forum maybe this should go to programming and computer science instead? Can I get a mod to help me out

edit: actually please delete this thread I have my answer now :) thank you
 
Last edited:

Similar threads

  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
Replies
15
Views
10K
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 15 ·
Replies
15
Views
6K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 22 ·
Replies
22
Views
3K