Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
    
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 '12 at 8:50
add comment

2 Answers

up vote 1 down vote accepted

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)

share|improve this answer
    
That is great! Is there any way to have all commands automatically wrapped? –  dotancohen Feb 3 '12 at 16:17
add comment

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"

share|improve this answer
    
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 '12 at 13:34
add comment

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.