Echo ps while preserving newlines?

Tyilo picture Tyilo · Jul 7, 2011 · Viewed 11.6k times · Source

If I do ps ax in Terminal, the result will be like this:

  PID   TT  STAT      TIME COMMAND
    1   ??  Ss     2:23.26 /sbin/launchd
   10   ??  Ss     0:08.34 /usr/libexec/kextd
   11   ??  Ss     0:48.72 /usr/sbin/DirectoryService
   12   ??  Ss     0:26.93 /usr/sbin/notifyd

While if I do echo $(ps ax), I get:

PID TT STAT TIME COMMAND 1 ?? Ss 2:23.42 /sbin/launchd 10 ?? Ss 0:08.34 /usr/libexec/kextd 11 ?? Ss 0:48.72 /usr/sbin/DirectoryService 12 ?? Ss 0:26.93 /usr/sbin/notifyd

Why?

And how do I preserve the newlines and tab characters?

Answer

Ignacio Vazquez-Abrams picture Ignacio Vazquez-Abrams · Jul 7, 2011

Same way as always: use quotes.

echo "$(ps ax)"