0

Apologies if this question is answered somewhere. If so, I will be more than happy to remove or edit this question. But I have searched long and hard for an answer to this (using Google since symbols don't work in StackExchange's search) but have been unable to find anything.

When searching for a command to sum numbers I stumbled upon this StackExchange answer that said the solution laid in a set of piped commands like the following:

<cmd> | paste -sd+ | bc

This seems to be correct, however, as a newer user to Bash scripting I have never seen the paste command be used with the options -sd+ before. The -s makes sense as the serial command, but everywhere I look says that the -d (delimiter) option must be used with a string after it to indicate the string that will be used as the delimiter. (e.g. something like -d "|".) Even the manual seems to indicate this:

Mandatory arguments to long options are mandatory for short options too.
 -d, --delimiters=LIST   reuse characters from LIST instead of TABs
 -s, --serial            paste one file at a time instead of in parallel
 -z, --zero-terminated    line delimiter is NUL, not newline
   --help     display this help and exit
   --version  output version information and exit

However, the command from the answer listed above does not have a string after it. Instead, it just has a + symbol (which I assume is for the arithmetic calculation in bc) and whitespace. And based on the context of the answer, the + symbol does not seem to be the new delimiter (espcially considering there is no space between it and d). What is the benefit of having -d attached if there is no delimiter specified?

So could someone please break down for me what the paste -sd+ command does? Thank you!

Guy
  • 979
  • 1
  • 11
  • 21

1 Answers1

2

paste -sd+ is just a shorter way of writing paste -s -d + or paste -s -d "+". So it is indeed the new delimiter.

$ printf '%s\n' 1 2 | paste -sd+
1+2
choroba
  • 231,213
  • 25
  • 204
  • 289
  • OH. I misunderstood how the delimiter worked in `paste`, in that case. I thought it was a delimiter for the **incoming** data that it would separate by. Now I understand, thank you! – Guy Apr 28 '21 at 06:14
  • (I'll accept your answer as soon as the cooldown on StackOverflow lets me) – Guy Apr 28 '21 at 06:14