I have a shell script that calls a command (sudo apt-get update). The current status is displayed in the terminal, but the older lines obviously move upwards. What I want to achieve is that the X recent lines are displayed and as a new line appears an old one is deleted. This means the output should always only occupy X lines.

I tried tail and sed but couldn't achieve the wanted result.

  • What exactly is the problem with allowing the output to scroll as it was intended to? – DepressedDaniel Dec 10 '16 at 18:26
  • There are other things in the script that i want to keep on the screen – jpscholz Dec 10 '16 at 18:42
  • Then you need ncurses. I've seen something like that in the builder for Armbian, where the apt-get output was supposed to be limited to a box in part of the terminal, but the feature was actually quite broken when I tried it, which only goes to demonstrate how very not the right thing to do it is. – DepressedDaniel Dec 10 '16 at 18:45
  • The right thing to do is decide you need a real UI, and your project is not just a terminal application anymore, and use wxPython or something to get a real UI. – DepressedDaniel Dec 10 '16 at 18:46
  • I feared that type of answer but I guess you are right. Thank you! – jpscholz Dec 10 '16 at 19:06

If you, like myself, is running the terminal session within tmux, then you can ask tmux to split the window and run another command in the new pane:

$ tmux split-window "sudo apt-get update"

The newly created pane will close as soon as the command has finished, so you may want to add a read afterwards (which will stall until you press Enter):

$ tmux split-window "sudo apt-get update; read"

You can read more about the split-window command in the tmux manual, but I'll just mention that if you add -lN, e.g. tmux split-window -l 5 ..., then you'll get a new pane with N rows (five in this example).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.