0

How To Echo Without Newline

Posted by Arch on June 12, 2019
how to echo without newline

The ‘echo’ command will always add a new line when you run it in a command console. This is convenient when you want to print out environmental variables and other pieces of information. It separates the individual pieces of information in the command and makes it easy to identify.

But, if you want to copy the output and use it in another console, the extra line may be an issue. Also, if you need to use the echo command but you want to build a CSV file, the invisible line can make all your efforts futile.

This article will explain how to use the ‘echo’ command without creating a new line for different platforms.

How to Echo Without Newline in Windows Command Prompt

If you have Windows 10, you can access Command Prompt to input your commands. There are ways where the new line can cause issues, especially if you want to copy the output and use it outside of Command Prompt.

So, if you type in ‘echo 1’ as the command in your prompt, you’ll get 1 as an output, followed by a new line and another input line.

new line

But if you want to use the same command without adding a new line, you need to type in additional commands after ‘echo’.

Let’s go over it step by step:

  1. Press ‘Windows’ and ‘R’ key at the same time to open the ‘Run’ window.
  2. Type ‘cmd’ in the Open box.
    cmd
  3. Type the following command in Command Prompt:
    echo | set /p=your text or variable (in this example it’s ‘1’)
  4. Press ‘Enter’ to execute this command.
  5. You should not see a new line in between.
    no new line
    If you want to copy the output to the clipboard, you’ll have to use the ‘echo’ command with the ‘clip’ command.
  6. Use the following code:
    echo | set /p=your text or variable|clip
  7. The ‘clip’ command will copy the text or variable to the clipboard.
  8. Open any text tool. For example, Notepad.
  9. Paste the clipboard to it.
  10. You should see your output in a string of text in Notepad.
    paste

How to Echo Without Newline 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.

When you want to write more complicated commands or sort everything in a single line, you should use the ‘-n’ option.

For example, if you input the code:
for x in ${array[@]}
do
echo $x
done| sort

The ‘echo $x’ command will sort the variables into separate lines. It may look something like this:
1
2
3
4
5

So, it won’t print the numbers on the same line.

There’s a way to display the output on a single line; you only have to use the ‘-n’ command.

It’d look like this:

for x in ${array[@]}
do
echo -n $x
done| sort

Hit return and you should see the numbers on the same line.

Echo with Printf Command in Bash

Another way to avoid adding a new line with ‘echo’ is to combine it with the ‘printf’ command.

For example, let’s use the following code:
NewLine=`printf “\n”`
echo -e “Line1${NewLine}Line2”

Without adding space after “\n”, you’ll get this result:

Line1Line2

However, if you add a space after “\n” like this:
NewLine=`printf “\n “`
echo -e “Line1{NewLine}Line2”

You’ll get the following result:
Line1
Line2

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. But if you want to add content directly to a text file via PowerShell, you should type the ‘-NoNewline’ command after the text or variable.

This is extremely useful for building a CSV file, for example. Or, if for some reason you need all your variables to remain on the same line.

Note that without the ‘-NoNewLine’ command, the will still automatically move to a new line after reaching the end of a line.

Thor’s Echo

Now that you know how to avoid adding a newline with echo, you can continue your coding.

If you know of other methods for accomplishing, don’t forget to share with the community in the comments. Many thanks in advance.

Leave a Reply

Your email address will not be published. Required fields are marked *


Disclaimer: Some pages on this site may include an affiliate link. This does not effect our editorial in any way.