Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
2  
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

2 Answers 2

up vote 39 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.