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?
Best Answer
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'secho -n
).Use
printf
:When
echo
does something unexpected, always considerprintf
. After you get familiar withprintf
it may even become your first choice.