Search code examples
bashprocessnewlineps

Echo ps while preserving newlines?


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?


Solution

  • Same way as always: use quotes.

    echo "$(ps ax)"