title: tcsh: foreach command works fine in Terminal, but doesn't work when called from a tcsh script file
body: <p><strong>For a reduced version of the problem, please first read Update1 below.</strong></p>
  
  <p>I've made a tcsh file to save to a file the list of all files/folders in the folder it's run (I'm working on a neuroimaging project, and I'm having the folder RawData containing a number of subjects each having a folder; I've written this file genSubjList.tcsh to save a list of subjects to the file subjList.txt). The code inside the file is:</p>
  
  <pre><code>foreach subj (*)
      echo $subj &gt;&gt; subjList.txt
  end
  </code></pre>
  
  <p>When I change to the RawData directory in Terminal and write this piece of code in Terminal, it generates the list of the subjects without any problem, but when I call genSubjList.tcsh using source command:</p>
  
  <pre><code>source ../scripts/genSubjList.tcsh
  </code></pre>
  
  <p>it just writes the name of the first folder (subject) in RawData to subjList.txt; I've already used chmode +x to make genSubjList.tcsh executable (the "scripts" folder is in the parent folder of RawData and contains genSubjList.tcsh)</p>
  
  <p>The output when I run the code inside terminal is the file subjList.txt containing:</p>
  
  <p>subject1
  subject2
  subject3 
  .
  .
  .
  subject n</p>
  
  <p>The output when I run the code using source ../scripts/genSubjLits.tcsh is the file subjList.txt containig:</p>
  
  <p>subject1</p>
  
  <p>I would highly appreciate any help in figuring out where I'm making a mistake. Thank you!</p>
  
  <p><strong>Additional explanation about why I want to figure out what the problem is:</strong></p>
  
  <p>The problem is not just about generating a list of subjects; I want to understand why this tcsh script doesn't work because the main problem is that I'm using FreeSurfer (surfer.nmr.mgh.harvard.edu) to analyze a number of subjects; I want to analyze them in parallel so I've developed some scripts to divide up the subjects in 5 groups (stored in 5 files named subjList1.txt .. subjList5.txt, each having a number of subjects). Then, I want to run the following code in 5 Terminals in parallel:</p>
  
  <pre><code>foreach subj (`cat subjList1.txt`)
      echo $subj
      recon-all -i $RAW_DATA/$subj/T1/*0001.dcm -subject $subj -all
  end
  </code></pre>
  
  <p>where subjList1.txt is substituted with subjList2, 3, etc for the parallel Terminals; the recon-all line does the analyses for each subject. Again, when I run this piece of code inside the Terminal, all subjects in each subjList are analyzed, but when I save the same piece of code in a number of script files (recon1.tcsh .. recon5.tcsh) and call them using:</p>
  
  <pre><code>source recon1.tcsh
  </code></pre>
  
  <p>just the first subject in each subjList is analyzed.</p>
  
  <p><strong>Update1:</strong></p>
  
  <p>Interestingly, if this simple loop:</p>
  
  <pre><code>foreach i (1 2 3 4 5)
      echo $i
  end
  </code></pre>
  
  <p>is stored in the file loop.tcsh, and is called using:</p>
  
  <pre><code>source loop.tcsh
  </code></pre>
  
  <p>the output will be just 1, meaning that it just goes through the first round of the loop. </p>
  
  <p>The same problem does not exist in BASH; if this loop:</p>
  
  <pre><code>for i in {1..5} ; do
      echo $i
  done
  </code></pre>
  
  <p>is stored in the file loop2.bash and is called using:</p>
  
  <pre><code>source loop2.bash
  </code></pre>
  
  <p>it works without any problem!</p>
  
  <p><strong>Update2:</strong></p>
  
  <p>The problem does not exist in BASH; if this code:</p>
  
  <pre><code>for subj in * ; do
      echo $subj &gt;&gt; subjList.txt
  done
  </code></pre>
  
  <p>is stored in the file genSubjList.bash and is called using:</p>
  
  <pre><code>source ../scripts/genSubjList.bash
  </code></pre>
  
  <p>in bash environment, it generates the list of the subjects without any problem!</p>