0

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
4

3 回答 3

3

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.

于 2013-05-29T17:38:34.303 回答
1

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
于 2013-05-29T17:55:28.277 回答
0

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.

于 2013-05-29T17:56:19.450 回答