66

What I need:

Suppose I have two commands, A and B, each of which returns a single-line string (i.e., a string with no newline character, except possibly 1 at the very end). I need a command (or sequence of piped commands) C that concatenates the output of commands A and B on the same line and inserts 1 space character between them.

Example of how it should work:

For example, suppose the output of command A is the string between the quotation marks here:

"The quick"

And suppose the output of command B is the string between the quotation marks here:

"brown fox"

Then I want the output of command(s) C to be the string between the quotation marks here:

"The quick brown fox"

My best attempted solution:

In trying to figure out C by myself, it seemed that the follow sequence of piped commands should work:

{ echo "The quick" ; echo "brown fox" ; } | xargs -I{} echo {} | sed 's/\n//'

Unfortunately, the output of this command is

The quick
brown fox
anubhava
  • 713,503
  • 59
  • 514
  • 593
synaptik
  • 8,359
  • 14
  • 66
  • 95

5 Answers5

81

You can use tr:

{ echo "The quick"; echo "brown fox"; } | tr "\n" " "

OR using sed:

{ echo "The quick"; echo "brown fox"; } | sed ':a;N;s/\n/ /;ba'

OUTPUT:

The quick brown fox 
anubhava
  • 713,503
  • 59
  • 514
  • 593
  • Thanks. Why doesn't `sed 's/\n//'` work in place of the `tr` command? – synaptik Jan 01 '14 at 18:13
  • @anubhava sed version does not work when there is more than two newlines, e.g. `{ echo "The quick"; echo "brown fox"; echo "something"; echo "else"; echo "more"; } | sed -e 'N;s/\n/ /'` – Aurelijus Rozenas Oct 28 '16 at 06:20
  • You can use a loop for that: `{ echo "The quick"; echo "brown fox"; echo "something"; echo "else"; echo "more"; } | sed ':a;N;s/\n/ /;ba'` – anubhava Oct 28 '16 at 17:48
51
echo "$(A)" "$(B)"

should work assuming that neither A nor B output multiple lines.

$ echo "$(echo "The quick")" "$(echo "brown fox")"
The quick brown fox
Mat
  • 195,986
  • 40
  • 382
  • 396
  • 1
    This answer is correct but proper quoting is required. ``echo "$(A)" "$(B)"`` and ``echo "$(echo "I love this asterisk sign")" "$(echo "*")"``. You can try it without quotes to see the problem – Aleks-Daniel Jakimenko-A. Jan 01 '14 at 18:45
  • what about multiple lines case ? – FuSsA Dec 12 '17 at 13:14
  • By wrapping the two commands in a single pair of quotes you choose to have one, several, or no spaces between the outputs, like `echo "$(A) $(B)"`. – Niemi Apr 27 '18 at 08:52
11

I'll try to explain the solution with another simple example

We've to concatenate the output of the following command:
"pwd" and "ls"

echo "$(pwd)$(ls)";

Output: 2 concatenated strings

hemanto
  • 1,602
  • 14
  • 16
10
$ commandA () { echo "The quick"; }
$ commandB () { echo "brown fox"; }
$ x="$(commandA) $(commandB)"
$ echo "$x"
The quick brown fox
chepner
  • 446,329
  • 63
  • 468
  • 610
3
$ { echo -n "The quick" ; echo -n " " ; echo "brown fox" ; }
The quick brown fox
Lirux
  • 95
  • 7