Welcome to the forum, firstly some housekeeping
...
To keep the forums high quality for all users, please take the time to format your posts correctly.
First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags
```text
and
```
by hand.)
Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.
Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.
Thank You.
The UNIX and Linux Forums
...
The variables var1 and var2 actually contain the literal strings you have assigned to them (i.e S*R1.fastq.gz
and S*R2.fastq.gz
) these are expanded by the shell when they are build into the echo command. To demonstrate:
$ var1=S*R1.fastq.gz
$ touch SecondR1.fastq.gz
$ echo "$var1"
S*R1.fastq.gz
$ echo $var1
SecondR1.fastq.gz
$ echo "${var1:1:1}"
*
See how using double quotes protects the string from being expanded by the shell. You could do something like this to have the shell expand the wildcard:
$ var1=S*R1.fastq.gz
$ touch SecondR1.fastq.gz
$ var1=$(echo $var1)
$ echo "$var1"
SecondR1.fastq.gz
$ echo ${var1:6:2}
R1
--- Post updated at 08:13 AM ---
One approach to this problem is to populate an array with the expanded filenames and then step thru it comparing with the next entry like this:
REST=.fastq.gz
file=( S*R[12]$REST )
for((i=0;i<${#file[@]};i++))
do
if [ "${file/%R1$REST/R2$REST}" = "${file[i+1]}" ]
then
echo ${file} and ${file[i+1]} are similar
((i++))
fi
done