Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid newline in echo

Tags:

powershell

Currently in PowerShell the echo command always append a newline. What would be the equivalent for the Unix echo -n ?

like image 782
mathk Avatar asked Feb 14 '12 16:02

mathk


People also ask

Does echo have newline?

Note echo adds \n at the end of each sentence by default whether we use -e or not. The -e option may not work in all systems and versions. Some versions of echo may even print -e as part of their output.

Which of the following option displays prompt without a new line?

Echo Command Options The echo command uses the following options: -n : Displays the output while omitting the newline after it.

How do you show and update echo on the same line?

The 2 options are -n and -e . -n will not output the trailing newline. So that saves me from going to a new line each time I echo something. -e will allow me to interpret backslash escape symbols.


2 Answers

echo is an alias for Write-Output which sends objects to the next command in the pipeline. If all you want to do is display text on the console you can do:

Write-Host "Hi" -NoNewLine

Keep in mind that this is not the same cmdlet as echo|Write-Output.

Write-Output's primary purpose is send objects to the next command in the pipeline where Write-Host's primary purpose is to display text on the console. The reason why you see text on the console using Write-Output is that the PowerShell engine sends everything to Out-Default at the end of the pipeline which sends the incoming to PowerShell's object to text formatting engine.

Here is an example:

Write-Host "hi" | Get-Member 

This will produce an error because Write-Host just writes the text to the console and doesn't forward the string to the next command in the pipeline.

Write-Output "hi" | Get-Member 

This will display the System.String properties and methods because Write-Output sends the string object to the next object in the pipeline.

  • Write-Host: http://technet.microsoft.com/en-us/library/dd347596.aspx
  • Write-Output: http://technet.microsoft.com/en-us/library/dd315282.aspx
like image 183
Andy Arismendi Avatar answered Sep 21 '22 01:09

Andy Arismendi


You can use Write-Host:

Write-Host "Hello " -nonewline Write-Host "There!" 

Or from the command line:

PS C:\> Write-Host "Hello " -nonewline ; Write-Host "There!" Hello There! 
like image 38
Mike Christensen Avatar answered Sep 19 '22 01:09

Mike Christensen