0

sed keeps giving invalid arithmetic operator error. I am trying to assign the output of sed to a variable.

This one works,

var=$(sed  -e 's/"currentGeneration":5010/"currentGeneration":5011/'  <<< $content )

but when I try to do the same with variable instead of 5010 and 5011, it fails with invalid arithmetic operator

var=$((sed  -e 's/"currentGeneration":$currg/"currentGeneration":$nextg/'  <<< $content ))

I tried below also but it won't substitute anything.

var=$(sed  -e 's/"currentGeneration":$currg/"currentGeneration":$nextg/'  <<< $content )

I am pretty sure I am missing something basic. Learning shell scripting on the job :(

Ram
  • 25

1 Answers1

3

In you command

var=$(sed  -e 's/"currentGeneration":$currg/"currentGeneration":$nextg/'  <<< $content )

$currg and $nextg are not expanded since they are not between double quotes.

To avoid the problem, you have a couple of options:

  • Use sed surrounded by double quotes and escape the inner double quotes:

    $ var=$(sed  -e "s/\"currentGeneration\":$currg/\"currentGeneration\":$nextg/"  <<< "$content" )
    
  • Concatenate single quotes with double quotes:

    $ var=$(sed  -e 's/"currentGeneration":'"$currg"'/"currentGeneration":'"$nextg/"  <<< "$content" )
    

Note that $content should be enclosed in double quotes.

(( is a compound command to evaluate arithmetic operations, so it's not doing what you intend to.