• Uncategorized

About unix : Replace-a-string-in-shell-script-using-a-variable

Question Detail

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?

Question Answer

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


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


pax> export replace=987654321
pax> echo X123456789X | sed "s/123456789/${replace}/"
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.

you can use the shell (bash/ksh).

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

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

# create some variables
# notice the the str isn't prefixed with $
#    this is just how this feature works :/
echo $result    
# result is: someFileName.bar

echo $result    
# result is: someFileName.sally because ".foo" was not found

Found a graceful solution.

echo ${LINE//12345678/$replace}

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"

Let me give you two examples.

  • Using sed:
echo $LINE | sed -e "s/12345678/$replace/g"
  • Without Using sed:
echo $result

Hope you will find it helpful!

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)

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

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"

Use this instead

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

this works for me just simply remove the quotes

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"

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.