Replace a string in shell script using a variable

UnixShellSed

Unix Problem Overview


I am using the below code for replacing a string inside a shell script.

echo $LINE | sed -e 's/12345678/"$replace"/g'

but it's getting replaced with $replace instead of the value of that variable.

Could anybody tell what went wrong?

Unix Solutions


Solution 1 - Unix

If you want to interpret $replace, you should not use single quotes since they prevent variable substitution.

Try:

echo $LINE | sed -e "s/12345678/${replace}/g"

Transcript:

pax> export replace=987654321
pax> echo X123456789X | sed "s/123456789/${replace}/"
X987654321X
pax> _

Just be careful to ensure that ${replace} doesn't have any characters of significance to sed (like / for instance) since it will cause confusion unless escaped. But if, as you say, you're replacing one number with another, that shouldn't be a problem.

Solution 2 - Unix

you can use the shell (bash/ksh).

$ var="12345678abc"
$ replace="test"
$ echo ${var//12345678/$replace}
testabc

Solution 3 - Unix

Not specific to the question, but for folks who need the same kind of functionality expanded for clarity from previous answers:

# create some variables
str="someFileName.foo"
find=".foo"
replace=".bar"
# notice the the str isn't prefixed with $
#    this is just how this feature works :/
result=${str//$find/$replace}
echo $result	
# result is: someFileName.bar

str="someFileName.sally"
find=".foo"
replace=".bar"
result=${str//$find/$replace}
echo $result	
# result is: someFileName.sally because ".foo" was not found

Solution 4 - Unix

Found a graceful solution.

echo ${LINE//12345678/$replace}

Solution 5 - Unix

Single quotes are very strong. Once inside, there's nothing you can do to invoke variable substitution, until you leave. Use double quotes instead:

echo $LINE | sed -e "s/12345678/$replace/g"

Solution 6 - Unix

echo $LINE | sed -e 's/12345678/'$replace'/g'

you can still use single quotes, but you have to "open" them when you want the variable expanded at the right place. otherwise the string is taken "literally" (as @paxdiablo correctly stated, his answer is correct as well)

Solution 7 - Unix

Let me give you two examples.

  • Using sed:
#!/bin/bash
LINE="12345678HI"
replace="Hello"
echo $LINE | sed -e "s/12345678/$replace/g"
  • Without Using sed:
LINE="12345678HI"
str_to_replace="12345678"
replace_str="Hello"
result=${str//$str_to_replace/$replace_str}
echo $result

Hope you will find it helpful!

Solution 8 - Unix

To let your shell expand the variable, you need to use double-quotes like

sed -i "s#12345678#$replace#g" file.txt

This will break if $replace contain special sed characters (#, \). But you can preprocess $replace to quote them:

replace_quoted=$(printf '%s' "$replace" | sed 's/[#\]/\\\0/g')
sed -i "s#12345678#$replace_quoted#g" file.txt

Solution 9 - Unix

I had a similar requirement to this but my replace var contained an ampersand. Escaping the ampersand like this solved my problem:

replace="salt & pepper"
echo "pass the salt" | sed "s/salt/${replace/&/\&}/g"

Solution 10 - Unix

Use this instead

echo $LINE | sed -e 's/12345678/$replace/g'

this works for me just simply remove the quotes

Solution 11 - Unix

I prefer to use double quotes , as single quptes are very powerful as we used them if dont able to change anything inside it or can invoke the variable substituion .

so use double quotes instaed.

echo $LINE | sed -e "s/12345678/$replace/g"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVijayView Question on Stackoverflow
Solution 1 - UnixpaxdiabloView Answer on Stackoverflow
Solution 2 - Unixghostdog74View Answer on Stackoverflow
Solution 3 - UnixbobView Answer on Stackoverflow
Solution 4 - UnixNicolae IotuView Answer on Stackoverflow
Solution 5 - UnixDaveView Answer on Stackoverflow
Solution 6 - UnixakiraView Answer on Stackoverflow
Solution 7 - UnixMd. Shahariar HossenView Answer on Stackoverflow
Solution 8 - UnixMatthieu MoyView Answer on Stackoverflow
Solution 9 - UnixRichard SaltView Answer on Stackoverflow
Solution 10 - UnixTunde PizzleView Answer on Stackoverflow
Solution 11 - UnixSteveScmView Answer on Stackoverflow