0

Hello I am trying to move a group of directories with the mv command and curly braces expansion as follows:

#!/bin/bash
...
group_name=$1
group=$((total/set))
group=$((group-1))
mkdir "$group_name""1" 
mv dat{10.."$group"} "$group_name""1"
...

The function is used as follows:

grouping_dirs total set group_name

e.g: grouping_dirs 30 2 dir_name

And the list of directories I have are:

dat10 dat11 dat12 dat13 dat14 dat15 and so on.

But I want to move only the directories dat{10..14} to dir_name1.

However I get the following error:

mv: cannot stat 'dat{10..14}': No such file or directory

Even though the directories exists. I know that because I tried changing:

mv dat{10.."$group"} ... to mv dat{10..14} ...

And it worked. I think I am not correctly using " " within the curly braces, but I am rather new to bash scripting...So I would appreciate your insight! Thanks!

1 Answers1

0

The problem is that in Bash, brace expansion happens before variable expansion. This means that brace expansion will not account for variables. You can use:

eval "echo dat{10..$group}"

It will give output as:

dat10 dat11 dat12 dat13 dat14

In your case use:

mv $(eval "echo dat{10..$group}") "$group_name""1"
Prvt_Yadav
  • 5,882