Will
Will

Reputation: 99

file name parameter expansion bash

I am trying to find all files with a specific extension and change them to a different extension. Both extensions will be put in as command line arguments. How would I set a variable equal to the file name before the "." using parameter expansion? I am new to scripting so any help would be greatly appreciated!

Upvotes: 1

Views: 526

Answers (1)

Barmar
Barmar

Reputation: 780673

name=${param%.*}

will set name to the value of $param without the extension.

From the bash manual:

${parameter%word}
${parameter%%word}
The word is expanded to produce a pattern just as in filename expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the value of parameter with the shortest matching pattern (the ‘%’ case) or the longest matching pattern (the ‘%%’ case) deleted.

Upvotes: 3

Related Questions