Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

What I get:

host:~ user$ cat example.txt
some texthost:~ stas$

What I want to get:

host:~ user$ cat example.txt
some text
host:~ stas$

Is there a way I can make cat behave like this?

I'm using bash on Mac OS X.

share|improve this question

3 Answers 3

up vote 6 down vote accepted

Most unix tools are designed to work well with text files. A text file consists of a sequence of lines. A line consists of a sequence of printable characters ending with a newline character. In particular, the last character of a non-empty text file is always a newline character. Evidently, example.txt contains only some text with no final newline, so it is not a text file.

cat does a simple job; turning arbitrary files into text files isn't part of that job. Some other tools always turn their input into text files; if you aren't sure the file you're displaying ends with a newline, try running awk 1 instead of cat.

You can make the bash display its prompt on the next line if the previous command left the cursor somewhere other than the last margin. Put this in your .bashrc (variation by GetFree of a proposal by Dennis Williamson):

shopt -s promptvars
PS1='$(printf "%$((COLUMNS-1))s\r")'$PS1
share|improve this answer
    
Thank you a lot for a working solution and succinct explanation! I understand this is a bit much for poor cat, so I'll keep this as a last resort for the time when this issue starts bothering me again. –  Stanislav Shabalin Jan 6 '13 at 22:46
    
Since this is bash preference, can it break piping commands? –  Stanislav Shabalin Jan 6 '13 at 22:51
1  
@StanislavShabalin This doesn't affect piping, only the prompt. –  Gilles Jan 6 '13 at 22:52
    
I need to remove the "-1" after "COLUMNS" for this to work correctly. –  rafak May 5 '13 at 6:23

The problem with that could be that your example.txt does not have a newline at the end of your file.

share|improve this answer
1  
The thing is I don't care if the file has a newline at the end or not. I wanna be able to see cat output more clear and not this disruptive :-) And I understand it's not cat's job so probably I'm looking for some workaround. –  Stanislav Shabalin Jan 6 '13 at 22:38

If you insist on using cat, this works for both types of files, with and without a newline at the end:

echo "`cat example.txt`"

You can turn it into a function with a name of your choice (even cat) in your .bashrc:

cat1(){ echo "`/bin/cat $@`";}
share|improve this answer
1  
Now that's a bit too much, even if it works ;-) Thanks though! –  Stanislav Shabalin Jan 6 '13 at 22:48

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.