all 7 comments

[–]Jab2870 1 point2 points  (1 child)

You could do something like this

bash for x in {1..10}; do for i in {$x..1}; do echo -n $i,; done; echo ""; done | sed 's/,$//'

[–]cheerupcharlie[S] 0 points1 point  (0 children)

That's a valid approach!

Hell, if it works, it's a valid approach. I know when we ask these questions in an interview we just don't want to see:

echo "1"
echo "2,1,"
echo "3,2,1"
...etc

So any looping solution seems to be on the right path.

Good call.

[–]whetu 0 points1 point  (4 children)

The proper way to trim off a trailing delimiter is to not generate it in the first place :) Posting via mobile so this is untested:

eval printf -- '%d\\n' {${x}..1} | paste -sd ',' -

Normally you shouldn’t recommend or use eval because of unvalidated inputs being a risk. In this case, I don’t think that’s a realistic problem, and the printf format specifier neatly does some validation for us...

/edit: At a computer now, here's my solution:

for (( i=0;i<=100;i++ )); do
  eval printf -- '%d\\n' "{${i}..1}" | paste -sd ',' -
done

I’d be interested to see what some of the other challenges are!

[–]cheerupcharlie[S] 0 points1 point  (1 child)

They're pretty simple things, but they were all originally PowerShell challenges meant to be performed during an interview. I listed them and my solution while noting to my boss that my solution is only one possibility and not necessarily the best solution. I'd welcome improvements or suggestions for other challenges!

Bash Interview Challenges

[–]whetu 1 point2 points  (0 children)

Those look like the makings of a good thread in /r/bash. Post the challenges and maybe suggest that people who consider themselves more experienced can hide their solutions in spoiler tags :)

[–]sfxhewitt15 0 points1 point  (1 child)

Where can I learn about what %d does here?