3

Just to give a quick example:

~$ cd ~/Documents/
~$ ls

a file   another file

~$ echo “Ok”

Ok

~$ rm *
~$ ls
~$ cd

This is inspired by this question, where trap 'echo' DEBUG is used to add an empty line before and after the output of every command. Unfortunately, empty lines are still added if the command had no output. Another downside of trap 'echo' DEBUG is that it is a bit ‘hacky’ and I’ve heard of it breaking some programs. It also adds empty lines between the output of commands in a pipe.

Now, I realise that it may not be possible to add an empty line before the output of a command because, to check if the command has any output, the output has to first be printed to the screen, and then you can’t add the empty line! Because of this it would also be OK if there’s an empty line between each prompt when running a command without output, just not that there’s two lines, like with trap 'echo' DEBUG.

The reason I’m doing this is I often find it difficult to find my prompt in a sea of output, especially when it is a colourful diff. I have tried two-line prompts, sometimes with empty lines before, and also adding a hideous mess of colours to my prompt. Neither has been quite satisfactory.

Thanks for the help!

  • Nothing comes to mind. You have varying control by setting the bash `PROMPT_COMMAND` environment variable, but without serous hackery won't do much more than you can do by adding the newlines in the prompt itself. My approach is simply to use a clean recognizable single-line prompt and let your eyes get used to finding it. Not an ideal answer, but without writing a separate shell capturing your commands and then evaluating whether output is produced (not trivial), there isn't a simple option available. – David C. Rankin Dec 02 '18 at 09:37
  • `echo -n | awk 'END{if(NR==0) {print;print}}1'`? – Cyrus Dec 02 '18 at 10:56
  • Did you try a different color for PS1, or maybe start PS1 with a newline? – Walter A Dec 02 '18 at 23:00

1 Answers1

1

You can simply do:

echo ""

Although for formatted outputs ending with a new line, printf is a better choice, for example : printf "%s\n\n" "output"

I think the following thread is on your topic, for empty lines: What is the preferred method to echo a blank line in a shell script?

As for controling the output try placing the command execution inside a variable and then evaluate the way you want.
For example with ls and if:

list=$(ls | wc -l)
if [ "$list" -gt 0 ]
then
        echo "Working fine"
else
        echo ""
fi

You can still print the output of the comand if you need to. But I think it is not necessary if there isn't some kind of reporting involved. For example to print the output in both cases:

Add the following to both when the condition is fulfilled and when it is not (execution of the command again):

echo "$(ls)"

For example altering the condition to not fulfill it gets the desired output:

adama@galactica:~$ ./processing.sh

a
column2.txt
pepito
processing.sh
test.txt

Best Regards

Ivo Yordanov
  • 146
  • 1
  • 8