Unwanted space in brace expansion

user3856370

I have the following brace expansion (bash shell):

echo -e {0..4..2}" "{0..2..2}"\n"

I expected this to produce

0 0
0 2
2 0
2 2
4 0
4 2

but every line of the output except the first has a leading space and there is an extra blank line at the end that I didn't expect. Why is this. Is there a simple way to fix it? Obviously I can do something clunky like pipe to sed 's/^ //', but is there a prettier way without piping to extra commands?

Kamil Maciorowski

echo prints its arguments separated by spaces, even if they include (or generate) newline characters. Additionally it adds one newline character at the end of its output (unless it's echo -n).

Use printf:

printf '%s\n' {0..4..2}" "{0..2..2}

When echo does something unexpected, always consider printf. After you get familiar with printf it may even become your first choice.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related