2

Is there any way to configure Bash to print a newline or horizontal line after commands that provided more than N lines of output? For instance, after a simple cd I don't need a newline or horizontal line, but after a long cat a visual indication of the end of each command's output would be great when I am comparing the output of various commands.

1
  • Maybe, I did not understand you well. If you want to add a newline, you can just use echo after each command.
    – Khaled
    Jan 4, 2012 at 8:50

2 Answers 2

2

Actually you could use a wrapper script like the one below:

#!/bin/bash

CMD="$*"
N=10
IFS=:

i=0;
eval $CMD | while read line; do
    echo "$line"
    i=$((i+1))
    if [ $((i % $N)) -eq 0 ]; then
        echo '================================================================';
    fi
done

Save it in your homedir, lets say as a ~/wrapper.sh. Make alias for it in your .bashrc file:

alias wrap="~/wrapper.sh "

Now you can invoke command of your choice with 'wrap' prefix:

wrap cat veryLongFile

stdout from your command will be post-processed by the wrapper.sh (I'm adding horizontal line after each 1o lines)

1
  • That is great! Is there any way to have all commands automatically wrapped?
    – dotancohen
    Feb 3, 2012 at 16:17
3

This cannot be done in bash. The program's output to the terminal does not pass through shell (bash), so there is no way for bash to determine how many lines were printed.

Personally I just manually hit enter once or twice in case when I need visual separation. If you would like to have a newline on every command, the best way is to export PS1='\n'"$PS1"

1
  • Thank you kubanczyk. I actually do currently have a newline character there but seeing a new line after every command is a real waste of screen space. It drives me nuts like source code with the opening bracket on a line to itself.
    – dotancohen
    Jan 4, 2012 at 13:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .