Search code examples
bashvariablessyntaxcygwincp

Why does my Cygwin copy or mkdir with a variable in directory name not work?


After building a Unity standalone build, I want to create some copies of both the executable and it's data folder, so I can run multiple instances of it. To do this easily, I've made this script:

for i in 1 2 3 4
    do cp SomeProject.exe SomeProject$i.exe
    cp -r SomeProject_Data/ SomeProject$i_Data
    echo i=$i done
done

However, while the copying of the executable works file, this folder copying doesn't. Instead of copying the datafolder to SomeProject1_Data, SomeProject2_Data, etc. It copies the first one to SomeProject and the next one inside that (and the following just overwrites the second, it seems).

I've tried inserting a mkdir SomeProject$i_Data and copying "into" that, but this also just creates SomeProject in the first iteration, and following iterations complain that SomeProject already exists.

Any idea what's going on/wrong?


Solution

  • I think I just worked it out myself. I need {} around the i in the variable. In my code $i_Data is seen as the variable name, which resolves to nothing/empty string. By adding {} around the i, it resolves correctly. So, working code is:

    for i in 1 2 3 4
        do cp SomeProject.exe SomeProject$i.exe
        cp -r SomeProject_Data/ SomeProject${i}_Data
        echo i=$i done
    done