110

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?

13
  • That would be slightly trickier to sort.
    – melpomene
    Jun 24, 2016 at 20:12
  • aside: shellcheck.net is your friend. Jun 24, 2016 at 20:21
  • ...also, "tried printf" doesn't show how you tried using printf. Details matter. :) Jun 24, 2016 at 20:23
  • 1
    ...btw, this is part of why we ask for questions to contain a minimal, complete, verifiable example -- to make this complete and verifiable it would have needed to include actual values for the array that reproduced the issue as part of the question, which would have meant we wouldn't have been fumbling around before figuring out your actual problem; see also stackoverflow.com/help/mcve. Jun 24, 2016 at 20:37
  • 1
    The canonical is "echo -n" prints "-n". Nov 5, 2021 at 14:43

5 Answers 5

199

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"
7
  • 4
    thanks it just prints out the '-n' instead before each number
    – ricky162
    Jun 24, 2016 at 20:12
  • 2
    Any reason to put the echo approach before the portable one? Jun 24, 2016 at 20:22
  • @CharlesDuffy As OP was using echo already, so thought about going with that first..
    – heemayl
    Jun 24, 2016 at 20:23
  • 2
    @ricky162, ...if you're seeing behavior other than that, then you necessarily have other problems -- such as data that doesn't represent what you put in the question; content with newline literals embedded in the array body, for instance. Jun 24, 2016 at 20:26
  • 1
    @ricky162 did you run the script with sh instead of bash? with bash it works as expected, with sh it prints literal -n
    – lilalinux
    Apr 22, 2022 at 12:40
10
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

5

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' ' '
4

If, for whatever reason, -n doesn't fix this for you, you can also add \c to the end of the thing to be echo'd:

echo "$x\c"
4
  • This is just as poorly portable as echo -n, and less widely supported.
    – tripleee
    Jun 27, 2022 at 11:41
  • Thanks, but I tried the -n in some cases and it did not work. This was the only way I could make it work. Jun 28, 2022 at 12:02
  • 1
    @tripleee actually, echo -n is NOT portable at all: unix.stackexchange.com/tags/echo/info - in short: echo -n will actually print -n on some systems, specifically on MacOS "sh". this answer is a valid option.
    – lzap
    Jun 28, 2022 at 12:12
  • @lzap That's precisely what I was saying. Both of these should be avoided in favor of the properly portable - as well as vastly more versatile - printf.
    – tripleee
    Jun 28, 2022 at 13:00
2

You can also do it this way:

array=(1 2 3 4 5)
echo "${array[@]}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.