First, Ruby uses /bin/sh
, not bash
, for the back tick operator. It is likely that on your system, /bin/sh
is a link to /bin/bash
, but bash
behaves differently when invoked as sh
, for better POSIX compliance. However, it's not perfect, as your example shows. The POSIX specification states that echo
"shall not support any options", but leaves handling of an initial argument -n
to be implementation-specific.
In a quick test with /bin/sh
as a link to /bin/bash
,
bash -c "echo -n foo"
and bash -c "echo -n foo | cat"
produced identical, newline-free results, but sh -c "echo -n foo"
and sh -c "echo -n foo | cat"
showed the results you report. (I am not sure how other shells, such as ksh
, dash
, or zsh
, behave when invoked as sh
, but by default they all treat -n
as a newline suppressor.)
For predictable results, use printf
instead, which never prints a newline unless a \n
is included in the format string.
printf 'foo\n'
printf 'foo'
printf 'foo' | cat
UPDATE: This appears to be a bug in bash 3.2
that was fixed at some point in the 4.x series. With 4.1 or later, sh -c "echo -n foo | cat
and sh -c "echo -n foo"
produce the same output.