0

I want this output: a b c d e

When I try:

for letter in {a..e}; do
  echo $letter 
done

I get this output:

a
b
c
d
e

I tried this:

for letter in {a..e}; do
  echo -e "\t$letter" 
done

Output:

      a
      b
      c
      d
      e

It only transfers it to whole line to next tab!

1 Answers1

4

Your Echo appends a newline by default after each run. You can

  • Supply the whole brace expansion as an argument, so that Echo runs a single time. It already separates its arguments by spaces.

    echo {a..e}
    
  • Use -n so as to not have a newline after each run, manually appending a space after the variable expansion.

    for letter in {a..e}; do
        echo -n "$letter " 
    done
    
  • Use Printf (Why is Printf better than Echo?).

    printf "%s " {a..e}
    
Quasímodo
  • 18,865
  • 4
  • 36
  • 73
  • 1
    @user432797 You are welcome. Yes, printf should not be in a loop if you don't want the output to be repeated 5 times. Only the 2nd alternative needs to be in a loop. – Quasímodo Sep 19 '20 at 21:44
  • 1
    @user432797 code highlighting in comments works with a single but it cannot have a space between the code and the symbol ... with spaces code and without spaces code – jsotola Sep 20 '20 at 00:21
  • @Quasímodo...thank you! I was trying to loop the command, in case of printf, when I loop it, it repeated the a to e 5 times : for letter in {a..e}; do printf "%s " {a..e} done output a b c d e a b c d e a b c d e a b c d e a b c d e – user432737 Sep 20 '20 at 13:17
  • @jsotola thank you!, I just updated the comment. – user432737 Sep 20 '20 at 13:17