0

My bash script

#!/bin/bash
read -r -p "Enter the filenames: " -a arr
for i in "${arr[@]}"
do
    echo "$i" | sed 's/\(.*\)\..*/\1/'

    # -- problem in this area -- 
    filenames="$i"
    #declare -a $filenames=$i
    # -- upto this line ---

    cp -v ~/Desktop/library/template.cpp "$filenames".cpp
done

With the help of this code echo "$i" | sed 's/\(.*\)\..*/\1/'
If number of inputs are A B.cpp C D.cpp, it will turn to A B C D

I need A B C D to go into for-loop and become A.cpp B.cpp C.cpp D.cpp

My problem: I'm not being able do that. (Failed)

  1. tried to implement latest echo $i which is A B C D
    cp -v ~/Desktop/library/template.cpp "$i".cpp
    didn't work

  2. tried another way. declaring the variable $i to the variable $filenames
    couldn't implement it correctly

Could anyone please help me out?

Mega Bang
  • 49
  • 1
  • 9
  • You appear to be asking [How can I assign the output of a command to a shell variable?](https://unix.stackexchange.com/questions/16024/how-can-i-assign-the-output-of-a-command-to-a-shell-variable) however there's a much simpler way to remove the suffix from the contents of variable `$i` using the shell's own parameter expansion `${i%.*}` – steeldriver Jan 14 '22 at 14:51
  • There's no need to delete your post; it can serve as a useful entry-point / signpost for future readers with similar problems. Thanks! – Jeff Schaller Jan 14 '22 at 15:09

0 Answers0