1

I would say that the safest option is to print the results without a new line since when the command is piped to another command which doesn't expect its input to be line separated you may run into troubles, like the following script:

function test_nl {
    echo result
}

function test_no_nl { printf %s result }

test_nl | xxd -p | wc -c #prints 15 test_no_nl | xxd -p | wc -c #prints 13

Are there any cases where the test_nl approach is better?

tturbox
  • 115
  • 4
    The purpose of your question is not clear. – FedKad Nov 14 '20 at 15:29
  • Using the test_no_nl(){ ... } syntax is better still. A two character answer to your question is no. – icarus Nov 14 '20 at 15:30
  • i prefer prefixing with function since it makes the code easier to read. Any benefits on the alternative syntax? @FedonKadifeli purpose is to see which is the ideal way of "returning" data from a function – tturbox Nov 14 '20 at 19:25
  • 1
    @tturbox I believe foo() { ... } is POSIX-portable, while function foo { ... } isn't. – JoL Nov 14 '20 at 23:34

2 Answers2

6

Depends. If you're always looking at what the exact output is, like with xxd above, then the newline may be just a nuisance. But for general use, consider that utilities like uname, uptime, date, and well, basically any such do print a trailing newline so that's definitely the custom. If a program run on the command line doesn't print that trailing newline, many shells will print the prompt on the same line, which looks ugly (below). Also, e.g. command substitution removes trailing newlines just to compensate for this.

bash$ date -u | tr -d '\n' 
Sat 14 Nov 2020 03:48:19 PM UTCbash$ 
ilkkachu
  • 138,973
  • If the prompt didn't start at the beginning of the line your line editing will get confused as to the position of the cursor – Chris Davies Nov 14 '20 at 16:05
  • @roaima, yes. that's the next problem, after the cosmetic one. – ilkkachu Nov 14 '20 at 16:27
  • and yes, Zsh is better in this regard, it prints a marker if the cursor is not left at start of line, and starts the prompt only on the next line. Something similar could probably be hacked for Bash. – ilkkachu Nov 14 '20 at 16:29
  • I actually like that bash doesn't do that. It allows a simple answer to questions like this, just like how you've provided. (Besides that zsh's behavior is a (nice) hack that console prompts generally shouldn't have to do). The real benefits of having a convention for newline-terminated output as opposed to newline-separated output are more nuanced and the question comes up often. I wrote a long answer somewhere once, but I think I'll hold off on doing it again until I put it on a blog. – JoL Nov 14 '20 at 23:52
2

Some cases where a trailing newline is preferred:

  • The output is meant to be human readable. Many prompts ($PS1s containing a carriage return) will overwrite some or all of the last line of output from a command unless it ends with a newline.
  • The output is a series of records, one per line. Parsing newline-terminated lines is easier than parsing newline-separated lines when using standard shell tools like read.
  • Logging, which ideally is just a special case of the above. That is, unless you're using some custom format which really isn't intended for human consumption.
l0b0
  • 51,350