# Gmx distance using bash for loop

**URL:** <https://gromacs.bioexcel.eu/t/gmx-distance-using-bash-for-loop/3617>\
**Category:** User discussions\
**Tags:** gmx-distance\
**Created:** [February 7, 2022, 10:31am UTC](https://gromacs.bioexcel.eu/t/gmx-distance-using-bash-for-loop/3617 "2022-02-07T10:31:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vignesh\_SR](https://dub1.discourse-cdn.com/flex017/user_avatar/gromacs.bioexcel.eu/vignesh_sr/32/2457_2.png) [@Vignesh\_SR](https://gromacs.bioexcel.eu/u/Vignesh_SR)\
**Post date:** [February 7, 2022, 10:31am UTC](https://gromacs.bioexcel.eu/t/gmx-distance-using-bash-for-loop/3617/1 "2022-02-07T10:31:59Z")

</div>

GROMACS version: 2019  
GROMACS modification: No

Dear All,  
I want to calculate com distance between two groups using gmx distance, using for loop bash scripting. My script

for (( i=26; i\<35; i++ ))  
do  
gmx distance -s md.tpr -f traj.xtc -n ind1.ndx -oall dist${i}.xvg -tu ns -select ‘com of group “$i” plus com of group 24’  
done

This gives the following error Message:

* * *

Program: gmx distance, version 2019  
Source file: src/gromacs/selection/selectioncollection.cpp (line 666)  
Function: void gmx::SelectionCollection::setIndexGroups(gmx\_ana\_indexgrps\_t\*)

## Inconsistency in user input: Invalid index group reference(s) Cannot match ‘group “$i”’, because no such index group can be found.

Simply “i” gives wrong data and I tried several ways for denoting the number in the group section to overcome this, but I failed to solve this issue. I request you, kindly help me to fix this issue. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![pjohansson](https://dub1.discourse-cdn.com/flex017/user_avatar/gromacs.bioexcel.eu/pjohansson/32/316_2.png) [@pjohansson](https://gromacs.bioexcel.eu/u/pjohansson)\
**Post date:** [February 7, 2022, 11:32am UTC](https://gromacs.bioexcel.eu/t/gmx-distance-using-bash-for-loop/3617/2 "2022-02-07T11:32:11Z")

</div>

Hi,

Try using double quotes around your `-select` command. With single quotes `$i` does not evaluate to your variable.

If you need double quotes inside of the command you should escape them, e.g. `-select "com of group \"$i\" plus com of group 24"`. (But you might not need to quote `$i` inside)

Regards,  
Petter

---

<div class="post-metadata">

**Author:** ![Vignesh\_SR](https://dub1.discourse-cdn.com/flex017/user_avatar/gromacs.bioexcel.eu/vignesh_sr/32/2457_2.png) [@Vignesh\_SR](https://gromacs.bioexcel.eu/u/Vignesh_SR)\
**Post date:** [February 7, 2022, 12:32pm UTC](https://gromacs.bioexcel.eu/t/gmx-distance-using-bash-for-loop/3617/3 "2022-02-07T12:32:01Z")

</div>

Thank you so much for your response, Sir.  
I used the following command,

gmx distance -s md.tpr -f traj.xtc -n ind1.ndx -oall dist${i}.xvg -tu ns -select “com of group “$i” plus com of group 24”

gave me the required result. Thanks again, Sir.

---

<div class="post-metadata">

**Author:** ![Vignesh\_SR](https://dub1.discourse-cdn.com/flex017/user_avatar/gromacs.bioexcel.eu/vignesh_sr/32/2457_2.png) [@Vignesh\_SR](https://gromacs.bioexcel.eu/u/Vignesh_SR)\
**Post date:** [February 7, 2022, 1:55pm UTC](https://gromacs.bioexcel.eu/t/gmx-distance-using-bash-for-loop/3617/4 "2022-02-07T13:55:11Z")

</div>

Hello All, I have an index file (attached), having 0-37 groups to calculate the com distance via bash for loop. Using that, first 9 and last one (37th) groups are not considered for the analysis, citing the following reasons

Script

for (( j=2; j\<=37; j++ ))  
do  
gmx distance -s md.tpr -f traj.xtc -n ind1.ndx -oall Dist${j}.xvg -tu ns -select "com of group “$j” plus com of group 0 " -b 99.995 -e 100  
done

```auto
Error: Multiple groups '2' selected

-------------------------------------------------------
Program: gmx distance, version 2019
Source file: src/gromacs/selection/selectioncollection.cpp (line 666)
Function: void gmx::SelectionCollection::setIndexGroups(gmx_ana_indexgrps_t*)

Inconsistency in user input:
Invalid index group reference(s)
  Cannot match 'group "2"', because no such index group can be found.
=================
  gmx distance -s md.tpr -f traj.xtc -n ind1.ndx -oall Dist37.xvg -tu ns -select 'com of group "37" plus com of group 0 ' -b 99.995 -e 100

-------------------------------------------------------
Program: gmx distance, version 2019
Source file: src/gromacs/selection/selectioncollection.cpp (line 666)
Function: void gmx::SelectionCollection::setIndexGroups(gmx_ana_indexgrps_t*)

Inconsistency in user input:
Invalid index group reference(s)
  Cannot match 'group "37"', because no such index group can be found.

```

[Index file](https://drive.google.com/file/d/13eEZoAr8LcLqR4wyHW9EZZ2SkO9b-igM/view?usp=sharing)

Kindly help me to solve this issue. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![jalemkul](https://dub1.discourse-cdn.com/flex017/user_avatar/gromacs.bioexcel.eu/jalemkul/32/18_2.png) [@jalemkul](https://gromacs.bioexcel.eu/u/jalemkul)\
**Post date:** [February 8, 2022, 2:40am UTC](https://gromacs.bioexcel.eu/t/gmx-distance-using-bash-for-loop/3617/5 "2022-02-08T02:40:04Z")

</div>

Do not enclose the variable `$j` in double quotes; these are used within selections to choose groups by name (string), e.g. `group "Protein"` is valid but `group "1"` is not.

---

<div class="post-metadata">

**Author:** ![Vignesh\_SR](https://dub1.discourse-cdn.com/flex017/user_avatar/gromacs.bioexcel.eu/vignesh_sr/32/2457_2.png) [@Vignesh\_SR](https://gromacs.bioexcel.eu/u/Vignesh_SR)\
**Post date:** [February 8, 2022, 6:39am UTC](https://gromacs.bioexcel.eu/t/gmx-distance-using-bash-for-loop/3617/6 "2022-02-08T06:39:24Z")

</div>

Thank you so much, Sir. Your advice helped a lot.
