Rhyknowscerious Rhyknowscerious - 9 months ago 50
Bash Question

Scroll a shell script output without piping it into less

I have a bash script.sh. I can easily scroll the output like this:

$ ./script.sh | less

But how do I make the output display scrollable automatically, without having to pipe it through
? In other words, how do I put that functionality right into the script itself? I just want to execute the script like this:

$ ./script.sh

I know I might be able to write a different script to execute the first one and pipe the output automatically but I don't want to have to write another script just to get the first one to do what I want it to do. Know what I mean?

Answer Source

You can write your script like this:


    Your script here

) | less

This will pipe the script output through less if output is a terminal (so you can ./script.sh > file without paging), and it preserves the script's exit code.