Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

However it may be impossible, I hope I'm just bad man reader =)

Is there any way to substitute text in variables on several patterns at time or even using back reference?

For example, I have FILE=filename.ext and I want to change it to filename_sometext.ext. But I don't know that file extension is .ext. All I know about it is that extension is after last dot. So I can do it in two steps:


Can I do it on one step (smth like ${FILE/.\(*\)/_sometext.\1} [that doesn't work])?

By the way I need to do it in pure shell without sed/awk/etc. My shell is ksh, but if there is way to do it with bashisms I'd like to know it too.

share|improve this question
I assume you mean ksh88 not ksh93? –  Mikel May 11 '12 at 8:55
What if the file name contains multiple dots? –  Mikel May 11 '12 at 9:13
I mean ksh93. Does it matter so much? And extension is after last dot, it doesn't matter if multiple dots appear before. –  rush May 11 '12 at 9:49
Yes, it matters, ksh88 is what most people mean when they say ksh, and it doesn't support many features that are specific to ksh93. Hence why I asked. ;-) –  Mikel May 11 '12 at 15:20

2 Answers 2

up vote 5 down vote accepted

Bash Parameter Expansion says that the variable (FILE in your example) must be a parameter name. So they don't nest. And the last part of ${param/pattern/replacement} must be a string. So back references aren't supported.

My only advice is to use


to avoid adding a trailing dot if the file has no extension.


Apparently back references are supported in ksh93.

So you could use something like

share|improve this answer
I got information about back reference and nested substitution. Thank you. –  rush May 11 '12 at 9:51

In this particular case I think FILE=${FILE%.*}_sometext.${FILE##*.} would do the job.

share|improve this answer
Yeah, it did the job, but I think my example was a little wrong for that. –  rush May 11 '12 at 9:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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