Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 380
Tyilo Avatar asked Jul 07 '11 19:07

Tyilo


People also ask

How do you echo break a line?

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.

How do you echo without newline?

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.

Why do we use echo with N?

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 (\).

How do I echo multiple lines in bash?

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.

Does Echo add newlines if you add new lines?

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.

How to avoid adding a new line with ‘Echo’ in PowerShell?

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.

How do I stop Echo in Bash?

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.

Should I see a new line in between my outputs?

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.


2 Answers

Same way as always: use quotes.

echo "$(ps ax)"
like image 108
Ignacio Vazquez-Abrams Avatar answered Oct 22 '22 08:10

Ignacio Vazquez-Abrams


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

like image 23
Jason Avatar answered Oct 22 '22 06:10

Jason