1

Suppose I have the following Bash script:

#!/bin/bash
INCREMENT_BY=5
sed 's/20000/&+$INCREMENT_BY/g' old > new

I expect all occurrences of 20000 to be replaced by 20005, but instead they are replaced with 20000+$INCREMENT_BY. How can I make this work?

1 Answers1

0

You should use double quote for eval variable, like:

sed "s/20000/$(( $INCREMENT_BY + 2000))/g" old
chengpohi
  • 14,064
  • 1
  • 24
  • 42
  • Hi, this makes the output "20000+5" instead of "20005". It seems to be interpreting it literally as a string, is there any way to make it evaluate the sum? –  Nov 16 '18 at 01:40
  • 1
    No. Sed can't do math. – that other guy Nov 16 '18 at 02:03
  • 1
    so since already know the **2000**, why can't directly **add**, like: `sed "s/20000/$(( $INCREMENT_BY + 2000))/g" old` – chengpohi Nov 16 '18 at 02:06