Prakhar Agrawal
Prakhar Agrawal

Reputation: 146

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  

Upvotes: 3

Views: 2907

Answers (5)

Leo
Leo

Reputation: 197

You could pipe the output to tr to simply transform the spaces into a newline:

$ echo {1..5} | tr ' ' '\n'
1
2
3
4
5

Upvotes: 1

tuto
tuto

Reputation: 11

Just put a backspace before the opening brace, like this: echo - e \\b{1..5}\\n

Upvotes: 1

Sebastien Lemayeur
Sebastien Lemayeur

Reputation: 11

Staying as close as possible to your original question, you could just pipe to sed. This simply gets rid of those 'unwanted spaces'.

echo -e {1..5}'\n' | sed 's/ //g'

General syntax for sed in this case: sed 's/REGEXP/REPLACEMENT/FLAGS' where 's' = substitute and '/' = delimeter

Upvotes: 1

Chem-man17
Chem-man17

Reputation: 1770

If you just want to output a list of numbers, seq is a dedicated tool-

$ seq 1 5 
1
2
3
4
5

Upvotes: 3

Meyer
Meyer

Reputation: 1722

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

Upvotes: 3

Related Questions