7
\$\begingroup\$

Let us play a little golfing game. Given a single line of N space separated numbers from STDIN, one must output those numbers on N lines.

So far, I came up with the three following bash solutions :

  • xargs -n1 (9)
  • tr \ \\n (8)
  • fmt -1 (6)

Can anyone do better? I am eager to learn new tricks

\$\endgroup\$
  • 7
    \$\begingroup\$ It is frowned upon to limit the language. \$\endgroup\$ – Leaky Nun Jul 6 '17 at 13:04
  • 1
    \$\begingroup\$ It is also frowned upon to restrict the format of the input and the output. \$\endgroup\$ – Leaky Nun Jul 6 '17 at 13:05
  • 3
    \$\begingroup\$ Welcome to PPCG. You should perhaps change this to a tips question. \$\endgroup\$ – officialaimm Jul 6 '17 at 13:07
  • 1
    \$\begingroup\$ Can the space-separated numbers be input as a string (enclosed with quote symbols)? Can the output lines contain leading spaces with the numbers right-aligned? Can we use functions that take a string and output a string with newlines? \$\endgroup\$ – Luis Mendo Jul 6 '17 at 16:41
  • 1
    \$\begingroup\$ bash or shell? all these are shell commands - not part of bash language. \$\endgroup\$ – philcolbourn Jul 12 '17 at 9:51
0
\$\begingroup\$

Bash (22)

read X;printf %d\\n $X  (thanks to manatwork but without literal NL)

30 read -aX;printf "%d\n" ${X[*]} 26 read X;echo -e ${X// /\n} 28 printf "%d\n" $(</dev/stdin) 27 printf "%d\n" $(</dev/fd/0) 26 printf %d\n $(</dev/fd/0) 23 read X;printf "%d\n" $X

\$\endgroup\$
  • 1
    \$\begingroup\$ As it says the input will contain just 1 line of numbers, read X;printf '%d␤' $X is enough. (Where ␤ is a literal line break.) \$\endgroup\$ – manatwork Jul 12 '17 at 10:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.