Hi I am new to bash scripting, trying to read a text file that contains my ids and want to use it in a loop by saving it to a $variable and using that for fetching some data.


file = "Cell_ids.text"

for i in file; do

         **id** = **$i**

sbatch --mem=4G --time=100 -o fetch_data.%j.out -e fetch_data.%j.err -J fetch_data  <<EOF
java -jar /docs/some-tools.jar --library ${**id**}

1 Answer 1


When declaring a variable, you aren't allowed to have spaces around the = sign, so your file variable isn't getting set. You also need to prefix it with a $ sign when you want to iterate through it.

Your for loop is also never closed, to signify the end of your loop, use the keyword done. I'm not sure what you want **id** = **$i** to do, but I'm guessing you want your script to look more like this:



for i in $(cat $file); do
    sbatch \
        --mem=4G \
        --time=100 \
        -o fetch_data.%j.out \
        -e fetch_data.%j.err \
        -J fetch_data << EOF
java -jar /docs/some-tools.jar --library $i

I removed the i=1 because it doesn't need to be declared before the loop since it will get set automatically. I'm not familiar with sbatch, but if you have an issue with that command, you may want to post a new question specifically about that.

  • See mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29. while IFS= read -r i; do ...; done < "$i" is the better approach.
    – terdon
    May 12 at 15:46
  • @terdon true, I made an assumption about the ids being alphanumeric and whitespace delimited. if I knew that was correct, does using some form of read still provide additional safety? May 12 at 16:19
  • 1
    See mywiki.wooledge.org/DontReadLinesWithFor for more details than you ever wanted to know! Yes, in this case it will probably be OK but it's a bad habit to get into since while will always work and this will often fail. So just always use while.
    – terdon
    May 12 at 16:32

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.