Bash brace expansion with string variables

19 views Asked by At

I have a series of files of the form:

test_0000.txt test_0001.txt ... test_0009.txt

and I would like to copy a subset of them to a sub directory

subdir/

It is my intention to use strings stored as variables and combine them to create the copy command, and use brace expansion to set the indexes I want to copy.

Here is my attempt at a solution:

prefix='test' 
suffix='.txt' 
imin=0 imax=9 
stride=4 
cp $prefix000{$imin..$imax..$stride}$suffix subdir/ 

however this yields:

cp: cannot stat '{0..9..4}.txt': No such file or directory 

There are two problems I see, the first being that $prefix and 000 might be interpreted as if I am referring to a variable $prefix000. The second being if I write this with quotations to avoid that issue:

cp $prefix'000'{$imin'..'$imax'..'$stride}$suffix' subdir/' 

then the brace expansion does not happen. My expected result from the command is:

cp test_0000.txt subdir/ 
cp test_0004.txt subdir/ 
cp test_0008.txt subdir/ 

Any help would be appreciated.

0

There are 0 answers