Could be a good time to learn pushd and popd!
I like using these instead of cd in scripts, that way if something breaks, it hangs onto context a little better. Also, when it runs successfully, you're right back in your original directory.
Note: A stumbling block you might encounter is building your "list" of directories. We can create this "list" by utilizing bash's pattern matching capabilities. For example, if we know that ALL the directories you'd like on your 'list' need to start with the pattern "test", we can simply append "test*" to your current working directory.
Let's say my cwd is /home/ben
and there are 5 dirs here [test1,test2,test3,test4,test5]
To create a full list (and simultaneously loop through that list), we can just do this:
for dir in /home/ben/test*; do cd ${dir}; pwd; done # Visit each dir in our little list
output:
/home/ben/test1
/home/ben/test2
/home/ben/test3
/home/ben/test4
/home/ben/test5
This little snippet does the same thing, but uses pushd and popd to traverse the list of test* dirs.
for dir in /{your_path_prefix_here}/test*; do
pushd ${dir} >/dev/null # We've arrived in this directory, and it's now in your stack. (hide the output, which displays your directory stack)
### Run whatever command you want, and then...
popd >/dev/null # Now you're right back where you were!
done