I have folders naming ouput00035023031 to output000035023035. I have written the bash script to iterate through each folder and enter the folder. After entering the folder I want to run a program 'barycorr'. Which asks for three inputs. The inputs are filenames *cl.evt. The code is blelow :

for i in $(seq -f "%03g" 1 5); do
    cd output0003502303$i
    echo -e "sw00092413002xpcw3po_cl.evt 
    sw00092413002sao.fits.gz" | barycorr ra=253.467570 dec=39.760169
    cd ..

I need to give first input the filenames sw*pcw3po_cl.evt, secondly the filename I want to save like sw*xpcw3po_cl_bary4.evt and third the path where sw*sao.fits.gz file is located. The * changes according to which folder I am in. How to write the script now to run this program inside the each folder.

  • Will there always be exactly one match to each of those patterns in each folder? That is, is it possible a folder might have sw1pcw3po_cl.evt and also sw2pcw3po_cl.evt? Jun 18, 2021 at 9:40
  • Yes, the sw00035023032xpcw3po_cl.evt changes according to output folder number like output00035023032 and aloing with this there might be file name sw00035023032xwtw2po_cl.evt i.e pcw3 may changed to 'wtw2'.
    – unicorn
    Jun 18, 2021 at 10:03
  • Is the first part always the same as the folder name? That is, are the files <foldername>xpcw3po_cl.evt, <foldername>xpcw3po_cl_bary4.evt, and <foldername>sao.fits.gz? Also, is it really necessary to cd into the folders, or would it work to pass paths (e.g. <foldername>/<foldername>xpcw3po_cl.evt) to the barycorr command? (The reason for the second question is that cd in scripts tends to be confusing, and can cause chain-reaction failures, so I prefer to avoid them.) Jun 18, 2021 at 10:17
  • Yes, we can give path . We can make list of this command by changing the numerical value only like 00035023030' to '00035023035'. : echo -e "/home/dinesh/Test/output00035023030/sw00035023030xwtw2po_cl.evt /home/dinesh/Test/output00035023030/sw00035023030xwtw2po_cl_bary.evt /home/dinesh/Test/00035023030/auxil/sw00035023030sao.fits.gz" | barycorr ra=253.467570 dec=39.760169 ` . but if one number is missed like say ..31 then it program throws error. I need to bypass the error too.
    – unicorn
    Jun 18, 2021 at 10:25

1 Answer 1


If I understand right, you should be able to do something like this:

for i in $(seq -f "%03g" 1 5); do
    printf '%s\n' "output0003502303${i}/sw00092413${i}"{xpcw3po_cl.evt,xpcw3po_cl_bary4.evt,sao.fits.gz} |
        barycorr ra=253.467570 dec=39.760169

Explanation: the printf '%s\n' will print each of the following arguments (the %s part) followed by newlines (the \n) -- that is, each on a separate line. printf is much more controllable for things like this than echo.

As for those arguments, each one consists of the folder name and filename prefixes, and then the {xpcw3po_cl.evt,xpcw3po_cl_bary4.evt,sao.fits.gz} part expands to each of those suffixes. Essentially, it's a shorthand for:


Is this what's needed? If not, please clarify.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.