user2433573
user2433573

Reputation:

Trouble altering a string in a variable with sed

I'm trying to get sed to update a variable in a bash script. Here is a simplified example of what I'm trying to do.

myword="this is a test"
echo $myword
this is a test

Swap a test for working

$myword | sed 's/a test/working'
echo $myword
this is working

Upvotes: 0

Views: 66

Answers (3)

chepner
chepner

Reputation: 532518

In bash, you don't need sed for this task:

$ myword="this is a test"
$ myword=${myword/a test/working/'}
$ echo $myword
this is working

Post your actual use case if you cannot adapt this solution.

Upvotes: 0

jaypal singh
jaypal singh

Reputation: 77185

Why go through the trouble of using sed. You can use bash string functions.

$ echo $myword
this is a test
$ echo ${myword/a test/working}
this is working

Upvotes: 1

mVChr
mVChr

Reputation: 50215

You need to terminate your regular expression:

myword="this is a test"
myword=`echo $myword | sed 's/a test/working/'`
echo $myword

-> this is working

Also, you never reassigned the output back to the myword var.

Upvotes: 3

Related Questions