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?
There are a couple of different ways we can print a newline character. The most common way is to use the echo command. However, the printf command also works fine. Using the backslash character for newline “\n” is the conventional way.
The best way to remove the new line is to add '-n'. This signals not to add a new line. When you want to write more complicated commands or sort everything in a single line, you should use the '-n' option. So, it won't print the numbers on the same line.
Echo Command Options -n : Displays the output while omitting the newline after it. -E : The default option, disables the interpretation of escape characters. -e : Enables the interpretation of the following escape characters: \\: Displays a backslash character (\).
To add multiple lines to a file with echo, use the -e option and separate each line with \n. When you use the -e option, it tells echo to evaluate backslash characters such as \n for new line. If you cat the file, you will realize that each entry is added on a new line immediately after the existing content.
An echo implementation which strictly conforms to the Single Unix Specification will add newlines if you do: But that is not a reliable behavior. In fact, there really isn't any standard behavior which you can expect of echo.
Another way to avoid adding a new line with ‘echo’ is to combine it with the ‘printf’ command. Without adding space after “n”, you’ll get this result: If you want all your input to print on the same line for some reason, you can always use the first example. What About PowerShell? Windows’ PowerShell doesn’t create a newline with the echo command.
Bash is the command console in Linux and Mac OS, which also recognizes the ‘echo’ command. In the case of Bash, echo also creates a new line in the output, but you can use different steps to stop it. The best way to remove the new line is to add ‘-n’. This signals not to add a new line.
You should not see a new line in between. If you want to copy the output to the clipboard, you’ll have to use the ‘echo’ command with the ‘clip’ command.
Same way as always: use quotes.
echo "$(ps ax)"
Simply use double-quotes in the variable that is being echo'd
echo "$(ps ax)"
this will do it without all that extra junk coding or hassle.
edit: ugh... someone beat me to it! lol
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With