0

I want to do this in a bash shell:

n=7
cp ~/file_{1..$n}.dat .

What I want it to execute is cp ~/file_{1..7}.dat ., but I know this isn't the proper way to do it. Can someone give me the correct format (without having to use a loop)?

gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
  • 1
    Short answer: you can't. Use a loop. – gniourf_gniourf Jan 14 '16 at 18:10
  • You can't without security risks (`eval`), that is. – Charles Duffy Jan 14 '16 at 18:10
  • ...and if the reason for not using a loop is avoiding the performance overhead of invoking `cp` more times than necessary, that's readily avoided by having your loop collect arguments in an array, and expanding that array into `cp`'s argument list. (Mind you, I mean an *actual* shell array, not a scalar variable misused as one; something like `files=( ); for ((i=1; i<=n; i++)); do files+=( ~/file_"$i".dat ); done; cp "${files[@]}" .`). – Charles Duffy Jan 14 '16 at 18:15
  • Maybe something like `cp $(seq -f '~/file_%g.dat' ${n}) .`? – twalberg Jan 14 '16 at 19:18
  • `cp ~/file_[1-7].dat .` – Walter A Jan 14 '16 at 22:45
  • @WalterA While I like that approach better when you have a fixed upper limit and only a single digit, the OP wanted to be able to use a variable for some reason, which is the reason for the more convoluted suggestion using `seq` that I made... – twalberg Jan 14 '16 at 23:05

0 Answers0