how do I echo $something >> file.txt without carriage return?

Solution 1:

That's what echo -n is for .

Solution 2:

printf is very flexible and more portable than echo. Like the C/Perl/etc implementations, if you do not terminate the format string with \n then no newline is printed:

printf "%s" "$something" >> file.txt

Solution 3:

If you are using command's output, you can use xargs in combination with echo

/sbin/ip route|awk '/default/ { print $3 }' | xargs echo -n >> /etc/hosts