bashshellbrace-expansion

How to use brace expansion with newline


I tried this method, but it shows an unwanted space at the beginning of new lines:

$ echo -e {1..5}"\n"  
1  
 2  
 3  
 4  
 5  

Solution

  • Brace expansion creates a space-separated list of strings. In your example, this means you get 1\n 2\n 3\n 4\n 5\n, which explains the space after each newline.

    To gain more control about the output format, you could use a loop:

    for i in {1..5}; do echo $i; done