3

Let's say we run two programs in the Linux shell, on one line, like this:

who -r; date

What should I add to obtain one line output?

4
  • 1
    ok, I got it. That works like a charm: echo who -r; date
    – biera
    Aug 30, 2012 at 22:19
  • should have been: echo `who -r; date`
    – biera
    Aug 30, 2012 at 23:06
  • You should attempt to ask an actual question in the title of your post since that will get you a lot more attention... Aug 31, 2012 at 13:53
  • Ok Max. I will consider this next time!
    – biera
    Sep 1, 2012 at 19:09

2 Answers 2

7
printf "%s %s\n" "$(who -r)" "$(date)"

Lots of quotes, but all are required. More simply:

echo $(who -r; date)

This one purposefully has no quotes.


Another option that does not require all the output from the commands to be captured: groups the commands together and pipe the combined output through paste

{ who -r; date; } | paste -s -d ' '

outputs something like

         run-level 3  Mar  1 19:44 Tue Apr 18 15:10:19 UTC 2023
0
echo -n `who -r`; echo -n ' '; date
2
  • 3
    If at all possible, get in the habit of using $( ... ) instead of `...`. That way, it's easy to capture the output of a command taht uses captured output. If you use backticks, it's almost impossible.
    – Vatine
    Aug 31, 2012 at 1:26
  • @Vatine plus, it's far far easier to read, and it doesn't confuse serverfault's parsing. Better all around. Aug 31, 2012 at 7:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .