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
    Does your terminal set the COLUMNS variable? would something as simple as foo | column -c $COLUMNS work in this case? – steeldriver Jul 9 '15 at 12:12

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.

  • @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. – klimpergeist Jul 9 '15 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... – don_crissti Jul 9 '15 at 14:20
  • I fixed the typo. – klimpergeist Jul 9 '15 at 14:41
  • If I don't export COLUMNS, neither version works for me if I use them in (non-interactive) scripts. – klimpergeist Jul 9 '15 at 14:42

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

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.