74

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
  • 761,203
  • 64
  • 569
  • 643
synaptik
  • 8,971
  • 16
  • 71
  • 98

5 Answers5

85

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
  • 761,203
  • 64
  • 569
  • 643
  • 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
54
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
  • 202,337
  • 40
  • 393
  • 406
  • 2
    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
14

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,900
  • 17
  • 16
11
$ commandA () { echo "The quick"; }
$ commandB () { echo "brown fox"; }
$ x="$(commandA) $(commandB)"
$ echo "$x"
The quick brown fox
chepner
  • 497,756
  • 71
  • 530
  • 681
  • 1
    This is the answer that worked for me. All the other ones did not. I just wanted to get the output of sensors on one line with a body of text I inserted with echo. – Matematleta Nov 05 '22 at 23:06
3
$ { echo -n "The quick" ; echo -n " " ; echo "brown fox" ; }
The quick brown fox
Lirux
  • 115
  • 8