Currently in PowerShell the echo
command always append a newline. What would be the equivalent for the Unix echo -n
?
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.
Echo Command Options The echo command uses the following options: -n : Displays the output while omitting the newline after it.
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.
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 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!
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