Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I 'echo' out things without a newline?

Tags:

bash

I have the following code:

for x in "${array[@]}"
do
  echo "$x"
done

The results are something like this (I sort these later in some cases):

1
2
3
4
5

Is there a way to print it as 1 2 3 4 5 instead? Without adding a newline every time?

like image 258
ricky162 Avatar asked Jun 24 '16 20:06

ricky162


People also ask

Does echo add a new line?

echo adds a newline by default.

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.

How do you escape a new line in Shell?

Escape character ( \ ) can be used to escape end of line, e.g. however, isn't end of line (new line) represented by \n which has two characters. shouldn't the result of the escape be the literal of \n . e.g.

What does echo E do?

Using the -e option allows you to use escape characters. These special characters make it easy to customize the output of the echo command. For instance, using \c let you shorten the output by omitting the part of the string that follows the escape character: echo -e 'Hello, World!

How to echo the same command without adding a new line?

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. 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:

Is there a way to add a final newline to echo-n?

The canonical is "echo -n" prints "-n". Yes. Use the -n option: This would strips off the last newline too, so if you want you can add a final newline after the loop: This is not portable among various implementations of echo builtin/external executable. The portable way would be to use printf instead:

Does echo without new line and with preceding space work?

You never stated that in your question or requested this behavior anywhere in your question. As stated by Pedro earlier, echo without new line and with preceding space works (provided "9" is a true [BackSpace]). I had some issues getting it to work in Windows 10 with the new console but managed the following way.

Does echo without new line work in Windows 10?

As stated by Pedro earlier, echo without new line and with preceding space works (provided "9" is a true [BackSpace]). I had some issues getting it to work in Windows 10 with the new console but managed the following way. (the actual symbol may vary depending upon codepage).


Video Answer


3 Answers

Yes. Use the -n option:

echo -n "$x"

From help echo:

-n do not append a newline

This would strips off the last newline too, so if you want you can add a final newline after the loop:

for ...; do ...; done; echo

Note:

This is not portable among various implementations of echo builtin/external executable. The portable way would be to use printf instead:

printf '%s' "$x"
like image 68
heemayl Avatar answered Oct 09 '22 02:10

heemayl


printf '%s\n' "${array[@]}" | sort | tr '\n' ' '

printf '%s\n' -- more robust than echo and you want the newlines here for sort's sake "${array[@]}" -- quotes unnecessary for your particular array, but good practice as you don't generally want word-spliting and glob expansions there

like image 6
PSkocik Avatar answered Oct 09 '22 03:10

PSkocik


You don't need a for loop to sort numbers from an array.

Use process substitution like this:

sort <(printf "%s\n" "${array[@]}")

To remove new lines, use:

sort <(printf "%s\n" "${array[@]}") | tr '\n' ' '
like image 4
anubhava Avatar answered Oct 09 '22 02:10

anubhava