In a bash script I want to show the user a file with less, but I don't want less to take up the whole terminal: I need to keep some informations visible in the terminal while the user browses the file with less.

I googled for terminal split linux and all the results refer to the screen command.

After several hours of trials end errors I believe I've come up with a mostly working solution. I've created a screen.rc file containing:

screen 1 less /etc/passwd
focus down
resize 10
screen 2
exec !.. echo Informations area
focus up

Now, in my bash script, I can run

screen -c screen.rc

and it creates a split terminal with less /etc/passwd output in the top region and Information area in the bottom one and the top region is receiving user input, which is exactly the intended behavior. Almost perfect, except when the user hits q to terminate less, screen does not terminate, because there's still a running shell in the bottom region. The user now needs to focus the bottom region (CTRL+a TAB) and hit Ctrl+d to terminate the running bash.

So the sequence to exit my custom less file viewer has now become q CTRL+a TAB CTRL+d: quite a lot of keystrokes only to exit a text file viewer...

How can I make screen terminate immediately when the top region less command quits?


Sending the quit command will tell screen to quit, so replace the less command with:

screen 1 sh -c 'less /etc/passwd ; screen -S "${STY}" -X quit'

or something to that effect.

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.