Comment

Comment by kentonv

It's pretty lame that the examples on the page show the answerer not just answering but also talking down to the questioner. XY is a problem that happens a lot, but disparaging people who are trying to learn is not the right response.

Here's how that conversation should have gone:

---

Q: How can I echo the last three characters in a filename?

A: If they're in a variable: echo ${foo: -3}

A: However, if what you really want is to get the file extension, it's probably better to handle extensions of any length. Try this instead: ${foo##*.}

A: That expression says to find the longest prefix of `foo` that matches the given glob pattern, and remove it.

Q: Oh wow! That is actually what I wanted, but it hadn't occurred to me that such powerful pattern-matching would be built into the shell. I already had a solution involving piping through sed, but it was pretty slow and ugly. I noticed that all the files I'm operating on have three-letter extensions and I figured there'd be an easier way to take a slice of a string, since that's a common thing to have built-in. But your answer is the best of both worlds. TIL!

---

There's really no need to admonish the questioner for starting with the wrong question. That won't help. It will only make them afraid to ask questions at all.

parent kentonv 7 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton