My bash script

read -r -p "Enter the filenames: " -a arr
for i in "${arr[@]}"
    echo $i | sed 's/\(.*\)\..*/\1/'
    cp -v ~/Desktop/library/template.py "$i".py

If i write A B.py C D.py, this script will turn it into A.py B.py C.py D.py
-v flag of cp -v explains what is being done

but i don't want to print the edited filenames echo $i. It's annoying.

How can i do that?
alternative solutions are also appreciated


2 Answers 2


You want to trim the value of $i after the last dot in the string.

You can do this like so:


This removes the shortest suffix string from $i that matches the filename globbing pattern .* (a dot followed by any string).

If you know you want to trim off .py specifically:


This means you could write your cp command like so:

cp -v ~/Desktop/library/template.py "${i%.py}.py"

This would be the only command in the loop body and it would remove the .py filename suffix from $i, if the value had such a suffix, and then add .py. This ensures that the destination filename always has .py as the filename suffix and at the same time avoids doubling the suffix.

Suggestion for script:


for string do
    cp -v ~/Desktop/library/template.py "${string%.py}.py"



for string; cp -v ~/Desktop/library/template.py $string:r.py

... which uses the zsh shell, its alternative, shorter, form for the for loop, and its :r modifier to remove the filename extension before adding .py to the name.

You would call either script like so:

./script A B.py C D.py
  • oh so much better.. thank you
    – Mega Bang
    Jan 14, 2022 at 15:06

I found an trick:

read -r -p "Enter the filenames: " -a arr
for i in "${arr[@]}"
    var=$(echo "$i" | sed 's/\(.*\)\..*/\1/')
    cp -v ~/Desktop/library/template.py "$var".py
  • isn't there any better solution? just assigning value to $i variable
    – Mega Bang
    Jan 14, 2022 at 11:31
  • I edit my answer, try this out
    – Konflex
    Jan 14, 2022 at 11:39
  • still shows.. i'm on parrot os
    – Mega Bang
    Jan 14, 2022 at 11:41
  • I reedited my answer, hope this one will suit you
    – Konflex
    Jan 14, 2022 at 14:41
  • 2
    the trick is called command substitution, and for various reasons, it's better to use the $() syntax than backticks. Also you need to quote the arg to echo to avoid various issues with the shell's word splitting and filename globbing.
    – ilkkachu
    Jan 14, 2022 at 16:49

Not the answer you're looking for? Browse other questions tagged .