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?
echo adds a newline by default.
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.
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.
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!
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:
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:
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.
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).
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"
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
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' ' '
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