2

I have a simple script

echo "Some Text"
echo " "
compgen -c a | sort | uniq | column
echo " "

This line set is repeated for each letter of the alphabet (not set in a loop) in 1stscript.sh.

I have a second lessscript.sh.

1stscript.sh | less

Running 1stscript.sh by itself fills the entire width of my terminal emulator:

But running lessscript.sh or 1stscript.sh | less directly only uses about 1/3 of my terminal emulator's width:

aa-exec         apropos         atrm
ab              apt             aubrsync
aclocal         apt-cache       aubusy

I have tried commenting out set text length lines. I have tried flags with column such as t and x to no avail.

How can I make it adapt to my terminal width in both cases?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578

1 Answers1

1

column will try to find the size of the terminal it's writing to, but if it's writing to a pipe (which has no inherent width), it defaults to 80.

The program tput does the same, but in addition to output, it checks stdin, stderr, and the session TTY. You can therefore use that to determine the column count for column:

echo "Some Text"
echo " "
compgen -c a | sort | uniq | column -c "$(tput cols)"
echo " "
that other guy
  • 116,971
  • 11
  • 170
  • 194