How do I print a variable in a shell script without altering trailing newline (if there is one)? <p>I have a shell script that takes input from another shell script. The stream gets piped in on stdin.</p> <p>I need to capture all the bytes of the stdin stream to a single variable in the shell script. Then, perform some operations on it, and send it back out over stdout.</p> <p>The problem I have is that sometimes there is a trailing newline character in the input file, but sometimes there is not. If there is not, then I do not want to add one. If there is a trailing newline, however, I want to preserve that. </p> <p>The problem is that no matter what I try, the system either always outputs WITHOUT a trailing newline (as in the case of <code>printf</code>) or it always outputs WITH a newline (as in the case of <code>echo</code>). </p> <p>Please tell me what is the name of a process (not <code>echo</code> or <code>printf</code>) that simply takes a variable and streams it out, verbatim, byte for byte, over stdout. I have tried all possible options for <code>printf</code> and none of them works to preserve trailing newlines. </p> <p>Please note I am not interested in why <code>printf</code> and <code>echo</code> both cannot do this one simple thing. Unless you can show me a portable way across unix platforms to use those commands to do what I ask, in which case I'm all ears :D</p>