2

Question:

I would like bash to print a blank line before the next prompt, but only if there has been output to the terminal.

Partial solution:

  • This answer proposes changing the bash custom prompt to PS1="\n$PS1". This results in bash producing a blank line before every terminal prompt.

  • This answer improves upon this slightly by preventing a newline before the first prompt. This is accomplished via the PROMPT_COMMAND bash variable and a function that sets a variable the first time it is called.

  • This question tries to accomplish something similar (a new line before output instead of after output), but the answers provided only produce newlines for every prompt.

Current behavior:

[user@host ~]$ ls
Desktop  Downloads  Dropbox  Music 

[user@host ~]$ vim 

[user@host ~]$ ssh login

[user@login ~]$ uptime
 08:01:11 up 50 days, 15:15, 15 users,  load average: 0.00, 0.00, 0.03

[user@login ~]$ vim file

[user@login ~]$ cd dir/foo

[user@login foo]$ mkdir bar

[user@login bar]$ cd bar

[user@login bar]$ vim file2

[user@login bar]$ ls
file2

[user@login bar]$

Desired behavior:

[user@host ~]$ ls
Desktop  Downloads  Dropbox  Music 

[user@host ~]$ vim 
[user@host ~]$ ssh login
[user@login ~]$ uptime
 08:01:11 up 50 days, 15:15, 15 users,  load average: 0.00, 0.00, 0.03

[user@login ~]$ vim file
[user@login ~]$ cd dir/foo
[user@login foo]$ mkdir bar
[user@login bar]$ cd bar
[user@login bar]$ vim file2
[user@login bar]$ ls
file2

[user@login bar]$

Motivation:

A new line improves readability, but it is only necessary when there is something to read.

9
  • 2
    This should be a shell feature, implementing it with external tools doesn't sound like a good idea Commented Oct 30, 2019 at 13:37
  • 2
    It's an interesting question. But I can't think of any reasonable mechanism by which the shell could even know whether the previous command produced any output. Commented Oct 30, 2019 at 13:52
  • I agree with @oguz. Do you think that using PROMPT_COMMAND and functions constitutes using "external tools"?
    – jmlarson
    Commented Oct 30, 2019 at 13:53
  • 1
    @Shardj: Even if you could, it would break basically all applications that interact with the user, if they are expecting for stdout to be a tty and it is a pipe instead. Commented Oct 30, 2019 at 17:25
  • 1
    Your motivation is improving readability. An alternative is a colored prompt. I use PS1="\[\033[1;34m\]\u@docker:\[\033[0m\]\w\n$\[\033[0m\] " on a dark blue blackground (and colorscheme pablo in .vimrc).
    – Walter A
    Commented Nov 1, 2019 at 9:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.