2

I would like to echo a list all in one line, TAB separated (like ls does with files in one folder)

for i in one two some_are_very_long_stuff b c; do echo $i; done

will print one line per word:

one
two
some_are_very_long_stuff
b
c

instead I would like to break it, like ls without options does:

mkdir /tmp/test
cd /tmp/test
for i in one two some_are_very_long_stuff b c z; do touch $i; done
ls

will output

b  one                       two
c  some_are_very_long_stuff  z
derobert
  • 109,670
rubo77
  • 28,966

2 Answers2

4

You could use the columns command from GNU autogen.

$ seq 60 | columns
1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
53 54 55 56 57 58 59 60

With zsh, you can use print -C:

$ print -C4 {1..20}
1   6   11  16
2   7   12  17
3   8   13  18
4   9   14  19
5   10  15  20

$ print -aC4 {1..20}
1   2   3   4
5   6   7   8
9   10  11  12
13  14  15  16
17  18  19  20

And if you need to sort them first (like ls does):

$ print -oC4 {1..20}
1   14  19  5
10  15  2   6
11  16  20  7
12  17  3   8
13  18  4   9
  • There is no s in column. – ctrl-alt-delor Nov 20 '13 at 13:26
  • @richard, we're not talking of the BSD column command here (from bsdmainutils on Debian based systems), but of the GNU columns command (from GNU autogen). – Stéphane Chazelas Nov 20 '13 at 15:08
  • ok. Columns is more powerful version of column, though I had to apt-get install autogen. column -x does the job for the simple case, the -x just changes the order from like first print example, to same as columns. So no spelling error, either will do. – ctrl-alt-delor Nov 20 '13 at 22:18
0

from the echo man page:

-n     do not output the trailing newline
-e     enable interpretation of backslash escapes
If -e is in effect, the following sequences are recognized:
   ...
   \t     horizontal tab

so this will do:

for i in one two some_are_very_long_stuff a b c d e f g h i j k l m n o; do 
   echo -en "$i\t";
done; echo
rubo77
  • 28,966