0

I'm trying to replace a substring in $1 with another, AND set it to a variable.

(saved as testStringReplacement file)

#!/bin/bash
secondString="Sara"
myString= "${1/Suzi/$secondString}"
echo "myString = $myString"

Then when I run this:

bash testStringReplacement Suzi

I get this:

testStringReplacement: line 3: Sara: command not found
myString = 

Can anyone see what I'm doing wrong? I want myString to be a string equal to the value of the string substitution (in this case, it should get set to "Sara"). It looks like it's doing the replacement, but trying to then execute it as a command.

ilkkachu
  • 138,973

1 Answers1

4

You have a space after the = in this:

myString= "${1/Suzi/$secondString}"

That will make it be interpreted as setting myString to blank and then try to run the command ${1/Suzi/$secondString}

It should be

myString="${1/Suzi/$secondString}"