0

This question already has an answer here:

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.

marked as duplicate by ilkkachu bash Dec 23 '18 at 21:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

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}"

Not the answer you're looking for? Browse other questions tagged or ask your own question.