2

I have a bash script foo that provides me with many short lines of output. Think ls -1. To get a more compact presentation, I would like to distribute the output over multiple columns, like ls -C.

I know that constructions like

foo | paste - - - | column -t -s $'\t'

will give me a fixed number of columns, in this case three. How can I dynamically adjust the number of columns to output width and terminal width such that I always use the maximal number of columns?

1
  • 1
    Does your terminal set the COLUMNS variable? would something as simple as foo | column -c $COLUMNS work in this case? Jul 9, 2015 at 12:12

2 Answers 2

2

The solution is actually almost too simple:

foo | column

without any arguments to column does the trick. I think I have never actively used column without -t before. Thanks to steeldriver for the suggestion to leave out this argument.

4
  • @don_crissti columns seems to automatically use the current value of $COLUMNS for all terminals I tried (putty, screen, xterm). At least it works without additional arguments no matter how I resize. Jul 9, 2015 at 14:15
  • Fwiw, it doesn't work for me (xterm, final term, gnome terminal) unless I export COLUMNS before running it... and it only works with column not with columns... Jul 9, 2015 at 14:20
  • I fixed the typo. Jul 9, 2015 at 14:41
  • If I don't export COLUMNS, neither version works for me if I use them in (non-interactive) scripts. Jul 9, 2015 at 14:42
0

Another option: echo $(foo) | fold where you can specify -w WIDTH to use something other than 80 columns.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .