I have a list of base files:

```
file1.txt 
file2.txt 
file3.txt 

```

and a list of target files: 

```
target1.txt
target2.txt 
target3.txt

```

and I want to use bash to perform the following command using a loop: 

```
grep -wf "file1.txt" "target1.txt" > "result1.txt" 
grep -wf "file2.txt" "target2.txt" > "result2.txt" 

```

The files will all have the same name besides the final integer, which will be in a series (1:22).
['https://Stackoverflow.com/questions/61958842', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/13456823/']
With arbitrary number of `file#.txt` and `target#.txt`:

```sh
#!/usr/bin/env bash

shopt -s extglob # Enable extended globbing patterns

# Iterate all file#.txt
for f in file+([[:digit:]]).txt; do
  # Extract the index from the file name by stripping-out all non digit characters
  i="${f//[^[:digit:]]//}"

  file="$f"
  target="target$i.txt"
  result="result$i.txt"

  # If both file#.txt and target#.txt exists
  if [ -e "$file" ] && [ -e "$target" ]; then
    grep -wf "$file" "$target" >"$result"
  fi
done

```
This is a one-line version suitable for the command line with brace expanion:

```
for i in {1..22};do grep -wf "file$i.txt" "target$i.txt" > "result$i.txt"; done

```