I am working on a small code in bash, but I am stuck on a small problem.
I have a string, and I want to replace the last letter of that string with s
.
For example: I am taking all the files that end in c
and replacing the last c
with s
.
for file in *.c; do
# replace c with s
echo $file
Can someone please help me?
In parameter substitution, ${VAR%PAT} will remove the last characters matching PAT from variable VAR. Shell patterns
*
and?
can be used as wildcards.The above drops the final character, and appends "s".
Use
rename
utility if you would want to get away with loopUse parameter substitution. The following accomplishes suffix replacement. It replaces one instance of
c
anchored to the right withs
.