Most of the time the output of a command ends with the newline character. But sometimes it does not, so the next shell prompt is printed in the same line together with the output.
root@hostname [~] # echo -n hello
helloroot@hostname [~] #
I've always found that very annoying.
Now, I could just add a "\n" at the beginning of the the PS1 variable, but most of the time that will print one extra line I dont need.
Is it possible to know whether the last command's output ended with a newline or not?
(Thanks to Dennis)
PS1='$(printf "%$((`tput cols`-1))s\r")\u@\h [\w]\$ '
$()in one place and backticks in another. You can use
tput colsbecause it just outputs the value of the COLUMNS variable anyway, and it's slower because it's not a shell builtin. You'll also want to include
tput el) to delete the inserted whitespace so that you don't get a bunch of trailing whitespace when copying and pasting in the default case. Finally, you need to enclose all this magic between
\]or else bash will try to second-guess your cursor position and it'll mess up when you edit your command/history.
PS1='\[\e[7m%\e[m$( printf "%*s" "$((COLUMNS-1))" "" )\r\e[K\]\u@\h [\w]\$ '