Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When I echo $something >> file.txt, a new line will be append to the file.

What if I want to append without a new line?

share|improve this question
Be careful doing echo $something, its behavior depends on the IFS variable, and you could end up with disappearing character. You can try the following: var="hello world"; echo $var (two spaces between hello and world) or var="hello world"; IFS='l'; echo $var or var="-e hello \\n world"; echo $var. To solve that, put double quotes around the variable like this: echo "$var", or use printf. – jfgagne Mar 1 '12 at 12:17
up vote 57 down vote accepted

That's what echo -n is for .

share|improve this answer
Is there an equivalent for cat? (e.g. when you have a file something.txt rather than a variable $something) – cboettig Nov 19 '13 at 23:05
@cboettig: No. Use a different tool to print everything but the final newline. – Ignacio Vazquez-Abrams Nov 19 '13 at 23:09

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
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .