So i trying to create my bash file that traverses through the current directory and replaces all occurrences of a string with a new one.

I have create this line and this works perfectly when running directly from Terminal but when its in a .sh file, it errors out.

```
cmd_sr="grep -rl 'REPLACESTRING' ./ | xargs sed -i '.bak' 's/REPLACESTRING/REPLACED/g'"`

```

And then executed the above line with `$cmd_sr` on the next line.

The following errors are shown.

```
grep: |: No such file or directory
grep: xargs: No such file or directory
grep: sed: No such file or directory
grep: '.bak': No such file or directory
grep: 's/REPLACESTRING/REPLACED/g': No such file or directory

```

Now I think I understand why this is happening, grep is trying to use the parameters after the './' as files, but obviously fails.

Hoping someone could point out the obvious error as im struggling to locate the issue.

=== EDIT ===

After some updates to the code and replacing it as a function... I have this

```
function sr {
    printf "Checking for $1 to replace with $2\n"
    grep -rl '%%APP_$1%%' ./ | xargs sed -i '.bak' 's/%%APP_$1%%/$2/g'
}

```

This function is called from a loop that does loops through a list of strings to replace in the directory structure. 

```
sr TITLE $title

```

Where $title is derived from the loop. As i need to check for a number of strings like `%%APP_xxxx%%` a function was the easier to implement. 

However with the updates made, I do not get an error now, but I also do not get an string replacements. Is it possible to view an output to see the files that it is looping through or perhaps finding these strings in?