How to Extract a File’s Extension in Bash: Simple Parameter Expansion
This article shows how to reliably obtain a filename’s extension in Bash using parameter expansion, explains why assuming a three‑character suffix can be wrong, and offers practical advice on providing clear information when seeking help with scripting problems.
No one can solve every problem alone, so we sometimes need help.
One day Noob needed to get the extension of a filename stored in the variable $foo in a Bash script.
Noob asked Feline how to obtain the last three characters of $foo.
Feline replied that echo ${foo: -3} would work, but asked why three characters were needed, suggesting it might be the extension.
Noob confirmed.
Feline warned that not all extensions are three characters and showed the proper way to extract the extension: echo ${foo##*.}.
This illustrates a typical XY problem.
Someone wants to achieve X.
They don’t know how to start and think achieving Y will solve X.
They also don’t know how to achieve Y.
They ask others for help to achieve Y.
Helpers find Y odd and are confused.
After much miscommunication, everyone realizes the person actually wants X, and solving Y doesn’t solve X.
What should be done?
Always provide as much information as possible so helpers understand the full context.
If helpers need more details, give them.
If you think certain solutions won’t work, explain why.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Node Underground
No language is immortal—Node.js isn’t either—but thoughtful reflection is priceless. This underground community for Node.js enthusiasts was started by Taobao’s Front‑End Team (FED) to share our original insights and viewpoints from working with Node.js. Follow us. BTW, we’re hiring.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
