Implement a command, that is the content of a variable.
I am trying to create a bash rename program that can rename all the files in a given directory. I have the following program:
for PATHFILE in tracks/*
do
path="$(dirname "$PATHFILE")"
file="$(basename "$PATHFILE")"
program='mv';
if [[ "$file" =~ ([0-9]*)\s*(.*) ]]
then
from=${path}/${file}
to=${path}/${BASH_REMATCH[1]};
command_string="$program '$from' '$to'";
#here the command_string contains the command i want to execute.
$(command_string);
#causes error:
# mv: target ''\''tracks/01'\''' is not a directory.
break;
fi
done
If I run the command directly myself then it runs without a problem. I also dont quite get why the system is adding those commas around the string.
If I echo the variable to the screen then i can just copy it, run and it runs without error. but if i try to run it in-code then i keep getting this error.
How do I get this to work?
command_string="$program '$from' '$to'";
– lese Jul 19 '18 at 12:10